TypeScript中的这些语句(接口与类型)有什么区别?
interface X {
a: number
b: string
}
type X = {
a: number
b: string
};
TypeScript中的这些语句(接口与类型)有什么区别?
interface X {
a: number
b: string
}
type X = {
a: number
b: string
};
当前回答
TypeScript手册给出了答案:
界面的几乎所有功能都是可用的类型。关键区别在于不能重新打开类型以添加新的财产vs始终可扩展的接口。
其他回答
类型示例:
//为对象创建树结构。由于缺少交集(&),您无法对接口执行相同的操作
type Tree<T> = T & { parent: Tree<T> };
//键入以限制变量仅分配几个值。接口没有联合(|)
type Choise = "A" | "B" | "C";
//由于类型,您可以通过条件机制声明NonNullable类型。
type NonNullable<T> = T extends null | undefined ? never : T;
界面示例:
//您可以使用OOP接口,并使用“implements”定义对象/类骨架
interface IUser {
user: string;
password: string;
login: (user: string, password: string) => boolean;
}
class User implements IUser {
user = "user1"
password = "password1"
login(user: string, password: string) {
return (user == user && password == password)
}
}
//可以使用其他接口扩展接口
interface IMyObject {
label: string,
}
interface IMyObjectWithSize extends IMyObject{
size?: number
}
就编译速度而言,组合接口的性能优于类型交集:
[…]接口创建检测属性冲突的单个平面对象类型。这与交叉点类型不同,在交叉点类型中,在对照有效类型进行检查之前,检查每个组成部分。接口之间的类型关系也被缓存,而不是交叉类型。
资料来源:https://github.com/microsoft/TypeScript/wiki/Performance#preferring-交叉口上的接口
除了已经提供的出色答案之外,在扩展类型和接口方面还有明显的区别。我最近遇到了一些界面无法完成任务的情况:
无法使用接口扩展联合类型无法扩展通用接口
接口与类型
接口和类型用于描述对象和原语的类型。接口和类型通常可以互换使用,并且通常提供类似的功能。通常由程序员选择自己的偏好。
然而,接口只能描述创建这些对象的对象和类。因此,必须使用类型来描述字符串和数字等原语。
下面是接口和类型之间的两个区别的示例:
// 1. Declaration merging (interface only)
// This is an extern dependency which we import an object of
interface externDependency { x: number, y: number; }
// When we import it, we might want to extend the interface, e.g. z:number
// We can use declaration merging to define the interface multiple times
// The declarations will be merged and become a single interface
interface externDependency { z: number; }
const dependency: externDependency = {x:1, y:2, z:3}
// 2. union types with primitives (type only)
type foo = {x:number}
type bar = { y: number }
type baz = string | boolean;
type foobarbaz = foo | bar | baz; // either foo, bar, or baz type
// instances of type foobarbaz can be objects (foo, bar) or primitives (baz)
const instance1: foobarbaz = {y:1}
const instance2: foobarbaz = {x:1}
const instance3: foobarbaz = true
何时使用类型?
通用转换
将多个类型转换为单个泛型类型时,请使用该类型。
例子:
type Nullable<T> = T | null | undefined
type NonNull<T> = T extends (null | undefined) ? never : T
类型别名
我们可以使用该类型为长类型或复杂类型创建别名,这些类型既难以阅读,又不方便反复键入。
例子:
type Primitive = number | string | boolean | null | undefined
创建这样的别名使代码更加简洁易读。
类型捕获
当类型未知时,使用该类型捕获对象的类型。
例子:
const orange = { color: "Orange", vitamin: "C"}
type Fruit = typeof orange
let apple: Fruit
在这里,我们得到了未知类型的橙子,称其为水果,然后使用水果创建一个新型的安全对象苹果。
何时使用界面?
多态性
接口是实现数据形状的契约。使用该接口明确表示它将被实现并用作关于如何使用对象的约定。
例子:
interface Bird {
size: number
fly(): void
sleep(): void
}
class Hummingbird implements Bird { ... }
class Bellbird implements Bird { ... }
尽管您可以使用类型来实现这一点,但Typescript更多地被视为一种面向对象的语言,并且接口在面向对象语言中具有特殊的地位。当您在团队环境中工作或为开源社区做出贡献时,使用界面更容易阅读代码。对于来自其他面向对象语言的新程序员来说也很容易。
官方的Typescript文档还说:
…我们建议尽可能在类型别名上使用接口。
这也表明该类型更适合于创建类型别名,而不是创建类型本身。
声明合并
您可以使用接口的声明合并功能向已声明的接口添加新的财产和方法。这对于第三方库的环境类型声明很有用。当第三方库缺少某些声明时,可以使用相同的名称再次声明接口,并添加新的财产和方法。
例子:
我们可以扩展上述Bird接口以包含新的声明。
interface Bird {
color: string
eat(): void
}
就是这样!记住什么时候使用比迷失在两者之间的细微差别中更容易。