typeOption<T>=Some<T>|None;interfaceSome<T>{_tag:'Some';value:T;}interfaceNone{_tag:'None';}typeComparable=number|Option<Comparable>;// ^^^^^^^^^^// Type alias 'Comparable' circularly references itself.经过刚才
一般来说,能用interface实现,就用interface,如果不能就用type Type Aliases的官方文档:https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-aliases Type aliases create a new name for a type. Type aliases are sometimes similar to interfaces, but can name primitives, unions, tuples, ...
};typeSetPoint=(x:number, y:number) =>void; 2. Other Types 与接口不同,类型别名还可以用于其他类型,如基本类型(原始值)、联合类型、元组。 // primitivetypeName=string;// objecttypePartialPointX= {x:number; };typePartialPointY= {y:number; };// uniontypePartialPoint=PartialPointX|PartialPointY...
// An interface cannot extend a primitive type like 'string'; an interface can only extend named types and classes // 'extends' clause of exported interface 'X' has or is using private name 'string'. interfaceXextendsstring { } 无法用interface定义的类型都使用type,比如基础类型、联合类型、元...
interface只是用来描述对象的形状,不能用来描述string等基础类型。而type是类型别名的意思,它相当于定义一个类型变量,可以声明任意类型。 typeEvenNumber=number;// 报错// An interface cannot extend a primitive type like 'string'; an interface can only extend named types and classes// 'extends' clause of...
interfaceA{good(x:number):string,bad(x:number):string}interfaceBextendsA{good(x:string|number):string,bad(x:number):number// Interface 'B' incorrectly extends interface 'A'.// Types of property 'bad' are incompatible.// Type '(x: number) => number' is not assignable to type '(x: ...
// Types of property 'bad' are incompatible. // Type '(x: number) => number' is not assignable to type '(x: number) => string'. // Type 'number' is not assignable to type 'string'. } 但使用交集类型时则不会出现这种情况。我们将上述代码中的接口改写成类型别名,把extends换成交集运算...
Types vs. interfaces in TypeScript interface X { a: number b: string } type X = { a: number b: string }; 我们可以用 interface 去 extend type: 用class 实现 type: 用class 实现 type 和 interface 的混合: type intersection 的用法,使用 & 连接多个 type: ...
type Point = { x: number; y: number; }; type SetPoint = (x: number, y: number) => void; 1. 2. 3. 4. 5. 6. 2. 都可以扩展 两者的扩展方式不同,但并不互斥。接口可以扩展类型别名,同理,类型别名也可以扩展接口。 接口的扩展就是继承,通过extends来实现。类型别名的扩展就是交叉类型,通过...
type 那个不叫 extend ,那就是类型合并 type 关键字的产生的东西官方有一个名字 type aliases ,就是类型别名,重点是它是别名不是真正的类型 我发现大家好像都不是非常喜欢去看文档,其实这种基本的东西根本不用去看 spec ,文档里面就应该有 https://www.typescriptlang.org/docs/handbook/advanced-types.html#typ...