编译器配置项-compilerOptions
输出相关配置项01
是否输出声明文件-declaration
在项目中为每一个TypeScript
或者JavaScript
文件生成声明文件.d.ts
。这些.d.ts
文件是类型定义文件,为你的代码描述了外部使用的API。使用.d.ts
文件,TypeScript
等工具可以为非类型化代码提供智能感知和准确类型。
当declaration
被设置为true
,TypeScript
编译器编译下边的TypeScript
代码时:
export let helloWorld = "hi";
将生成一个如下的index.js
文件:
export let helloWorld = "hi";
和一个对应的helloWorld.d.ts
:
export declare let helloWorld: string;
如果.d.ts
和JavaScript
文件一起工作,那么应该开启emitDeclarationOnly
功能,或者配置outDir
以确保JavaScript文件避免被覆盖。
声明目录-declarationDir
提供了一种配置发出声明文件的根目录的方法。
example
├── index.ts
├── package.json
└── tsconfig.json
配合这个tsconfig.json
文件:
{
"compilerOptions": {
"declaration": true,
"declarationDir": "./types"
}
}
index.ts
文件生成的声明文件.d.ts
将被放置在types
目录:
example
├── index.js
├── index.ts
├── package.json
├── tsconfig.json
└── types
└── index.d.ts
声明文件映射-declarationMap
为声明文件.d.ts
生成源映射文件,该映射文件指向原始的.ts
文件。这将允许 VS Code 等编辑器在使用“转到定义”等功能时转到原始.ts
文件。
如果在项目中使用了references
,那么强烈建议开启这个功能。
网友评论