1.ts提供静态代码分析:检查代码结构和类型注解;
2.当声明了一个索引签名时,所有明确的成员都必须符合索引签名
interface IProps {
[key: string]: string;
x: string;
y: string;
// y: number; // 报错
}
- 项目中的声明文件必需包含在 tsconfig.js的include中,不然*.d.ts声明文件不生效
- 三斜线指令 参考
/// <reference path="./node.d.ts" />
/// <reference types="node" />
path 类型声明的是对本地文件的依赖,包含路径信息。
types 类型声明的是对 node_modules/@types 文件夹下的类型的依赖,不包含路径信息。
常常在一个后缀名为 .d.ts 的声明文件中可以见到三斜线指令引入文件,上面的在编译阶段,被依赖文件 node.d.ts 将被包含进来,就像将被依赖文件的源码展开在依赖声明处一样。
- baseUrl 与 paths 结合使用,paths是相对于 baseUrl 的, 假如目录是:
--src
--node-modules
{
"compilerOptions": {
"baseUrl": "./src", // This must be specified if "paths" is.
"paths": {
"jquery": ["../node_modules/jquery/dist/jquery"] // 此处映射是相对于"baseUrl"
}
}
}
6、对象字面量 会进行严格的类型检查,例子:
interface IX {
foo: number;
}
const x1 = { foo: 1, baz: 2 };
// const x: IX = { foo: 1, baz: 2 }; // 报错 对象字面量 会严格检查类型
const x: IX = x1; // ok 复制的值x1满足接口 IX,允许赋值
let y: { foo: number; bar?: number };
y = { foo: 1, baz: 2 }; // Error, excess or misspelled property `baz`
console.log(x, y);