美文网首页
TSConfig文件详解17

TSConfig文件详解17

作者: 从零开始学ArchLinux | 来源:发表于2024-07-02 23:00 被阅读0次

编译器配置项-compilerOptions

输出相关配置项01

是否输出声明文件-declaration

在项目中为每一个TypeScript或者JavaScript文件生成声明文件.d.ts。这些.d.ts文件是类型定义文件,为你的代码描述了外部使用的API。使用.d.ts文件,TypeScript等工具可以为非类型化代码提供智能感知和准确类型。

declaration被设置为trueTypeScript编译器编译下边的TypeScript代码时:

export let helloWorld = "hi";

将生成一个如下的index.js文件:

export let helloWorld = "hi";

和一个对应的helloWorld.d.ts

export declare let helloWorld: string;

如果.d.tsJavaScript文件一起工作,那么应该开启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,那么强烈建议开启这个功能。

相关文章

网友评论

      本文标题:TSConfig文件详解17

      本文链接:https://www.haomeiwen.com/subject/zjkbcjtx.html