接口
1 | interface LabelledValue { |
可选属性
带有可选属性的接口与普通的接口定义差不多,只是在可选属性名字定义的后面加一个 ?
符号。
1 | interface SquareConfig { |
只读属性
1 | interface Point { |
TypeScript具有ReadonlyArray
1 | let a: number[] = [1, 2, 3, 4]; |
上面代码的最后一行,可以看到就算把整个ReadonlyArray赋值到一个普通数组也是不可以的。 但是你可以用类型断言重写:
1 | a = ro as number[]; |
readonly vs const, 变量使用的话用 const,属性则使用readonly。
额外的属性检查
1 | interface SquareConfig { |
如果一个对象字面量存在任何“目标类型”不包含的属性时,你会得到一个错误.即你传入的对象自变量中出现了配置中不包含的属性,那么就会触发 额外的属性检查
并提示错误。
虽然witdh已经确定,但是colour并不是 SquareConfig
想要的值,ts会判定这里有一个错误。
绕过的方式可以使用断言:
1 | let mySquare = createSquare({ width: 100, opacity: 0.5 } as SquareConfig); |
最佳的方式是能够添加一个字符串索引签名,
1 | interface SquareConfig { |
还是可以考虑进行重新复制绕过:
1 | let squareOptions = { colour: "red", width: 100 }; |
函数类型
1 | interface SearchFunc { |
1 | let mySearch: SearchFunc; |
索引签名
我们可以明确的指定索引签名。例如:假设你想确认存储在对象中任何内容都符合 { message: string } 的结构,你可以通过 [index: string]: { message: string } 来实现。
1 | const foo: { |
1 | interface StringArray { |
索引签名的名称(如:{ [index: string]: { message: string } } 里的 index )除了可读性外,并没有任何意义。例如:如果有一个用户名,你可以使用 { username: string}: { message: string },这有利于下一个开发者理解你的代码。
所有成员都必须符合字符串的索引签名
1 | interface NumberDictionary { |
TypeScript支持两种索引签名:字符串和数字。 可以同时使用两种类型的索引,但是数字索引的返回值必须是字符串索引返回值类型的子类型。 这是因为当使用 number来索引时,JavaScript会将它转换成string然后再去索引对象。
1 | class Animal { |
Class 类型
1 | interface ClockInterface { |
Difference between the static and instance sides of classes
1 | interface ClockConstructor { |
继承接口
1 | interface Shape { |
Hybrid类型
接口能够描述JavaScript里丰富的类型。 因为JavaScript其动态灵活的特点,有时你会希望一个对象可以同时具有上面提到的多种类型。
1 | interface Counter { |
接口继承类
当接口继承了一个类类型时,它会继承类的成员但不包括其实现
1 | class Control { |
Interfaces inherit even the private and protected members of a base class. This means that when you create an interface that extends a class with private or protected members, that interface type can only be implemented by that class or a subclass of it.
如果接口继承的父类包含了私有属性,那么该接口就只能被 这个类或其子类所实现。
因为接口中存在了private属性,但是 ImageControl
中定义的 state
是自己的,所以会和 接口中的私有 state 冲突。
但是如果不定义,则又会缺少参数。所以导致了存在私有属性只有这个类或其子类所实现,这样就可以保证当前的类中 实现 接口中的 private
属性。
1 | class ImageControl extends Control implements SelectableControl { |