TypeScript 是 JS 的超集,重点主要是给脚本语言增加了强类型支持,在构建时就发现代码的类型造成的错误,增强代码的稳定性,仅此而已
可索引声明
interface SquareConfig {color?: string;width?: number;[propName: string]: any; // 不确定还会传进什么的时候}class xxx implements SquareConfig{color: 'aaaa';width: 222;xxx: 'sss'}
类和函数相关泛型高级使用方法
- 使用 extends指定泛型类型的继承关系 ``` interface Lengthwise { length: number; }
function loggingIdentity
loggingIdentity({ length: 2, value: 3 });
2. 指定泛型类型为类类型的约束
function create
3. 一个更高级的例子,使用原型属性推断并约束构造函数与类实例的关系
class ZooKeeper { nametag: string; }
class Animal { numLegs: number; }
class Lion extends Animal { keeper: ZooKeeper; }
function createInstance(c: new () => A): A { return new c(); } createInstance(Lion).keeper.nametag; // typechecks!
参考:[TypeScript 泛型使用整理](http://www.voidcn.com/article/p-qyoknmht-bhs.html)<a name="8d633aea"></a>## xx.d.ts 和 interface在 Antd 的代码里经常能看到 d.ts 的代码,这个文件是什么作用?通俗意义上就是给 IDE 或者人一个可读的对外的数据、接口文档,起到辅助作用(尤其是 IDE 的代码提示),实际上并不会用到。那么我们在代码里定义的 interface 源代码不是能够直接给别人看嘛(只不过看起来麻烦点)?显然从理论上是没问题的,就是不干净,通过构建成 d.ts 可以去除与声明代码无关的业务代码或者注释,会更干净点,例如:xx.ts```typescriptexport const funA: () => boolean = () => {return true;};// 这里是注释export interface ObjectX {id: number;title: string;}
xx.d.ts
export declare const funA: () => boolean;export interface ObjectX {id: number;title: string;}
参考:TypeScript 中的 .d.ts 文件有什么作用,这种文件的内如如何编写?
