简体   繁体   English

在 tsconfig.json 中启用声明和声明映射的用例是什么?

[英]What is the use-case of enabling declaration and declarationMap in tsconfig.json?

If we enable sourcemap which allows us to debug on the browser.如果我们启用允许我们在浏览器上调试的源映射。 Likewise looking for the use case of declaration and declarationMap.同样寻找声明和声明映射的用例。

I have searched over the internet, but my bad could not found the actual use case except generating.d.ts file.我已经在互联网上进行了搜索,但是除了 generate.d.ts 文件之外,我的错误找不到实际的用例。

Generating .d.ts files is exactly the use case for declaration compiler option, so the build output is .js , while all type definitions can be still preserved for other TS projects consuming your project.生成.d.ts文件正是declaration编译器选项的用例,因此构建 output 是.js ,而所有类型定义仍然可以保留给使用您项目的其他 TS 项目。

The IDE can make use of declarationMap option to provide better developer experience: It enables you to quickly navigate to the original sources, when you currently have a corresponding .d.ts file open and want to see its implementation (see also this answer ). IDE 可以使用declarationMap选项来提供更好的开发人员体验:当您当前打开相应的.d.ts文件并希望查看其实现时,它使您能够快速导航到原始源(另请参阅此答案)。

Enabling --declarationMap alongside --declaration causes the compiler to emit.d.ts.map files alongside the output.d.ts files.在 --declaration 旁边启用 --declarationMap 会导致编译器在 output.d.ts 文件旁边发出.d.ts.map 文件。 Language Services can also now understand these map files, and uses them to map declaration-file based definition locations to their original source, when available.语言服务现在还可以理解这些 map 文件,并在可用时将它们用于基于 map 声明文件的定义位置到其原始源。

In other words, hitting go-to-definition on a declaration from adts file generated with --declarationMap will take you to the source file (.ts) location where that declaration was defined, and not to the.d.ts.换句话说,对使用 --declarationMap 生成的 adts 文件中的声明点击 go-to-definition 会将您带到定义该声明的源文件 (.ts) 位置,而不是.d.ts。

As your application grows, it will take longer and longer for the Typescript to typecheck and compile your code.随着您的应用程序的增长,Typescript 将花费越来越长的时间来检查和编译您的代码。 With a large codebase, slow compile times can seriously slow down your development.对于大型代码库,缓慢的编译时间会严重减慢您的开发速度。 To overcome this, Typescript 3.0 onward, you can split your big project into smaller sub-projects using Project References .为了克服这个问题,从 Typescript 3.0 开始,您可以使用Project References将您的大项目拆分为更小的子项目。 declaration and declarationMap options play a big role here. declarationdeclarationMap选项在这里发挥了重要作用。


declaration option declaration选项

Enabling the declaration option will result in Typescript compiler creating the declaration files ( .d.ts ).启用declaration选项将导致 Typescript 编译器创建声明文件 ( .d.ts )。 The .d.ts files contain the declarations of the types used in your corresponding .ts files. .d.ts文件包含相应.ts文件中使用的类型的声明。 They don't contain the implementations of the types, they just contain the publicly accessible type declarations.它们不包含类型的实现,它们只包含可公开访问的类型声明。

So, anyone can use the types from your Typescript project in some other Typescript project.因此,任何人都可以在其他一些 Typescript 项目中使用 Typescript 项目中的类型。 The compiler can typecheck the code in that other project with the help of your .d.ts files even though they don't have the access to your project.编译器可以借助您的.d.ts文件对其他项目中的代码进行类型检查,即使它们无权访问您的项目。

This is the thing that helps when you split a big project into smaller multiple sub-projects.当您将一个大项目拆分为多个较小的多个子项目时,这会有所帮助。 Sub-projects have access to each other's declaration files.子项目可以访问彼此的声明文件。 When one of your sub-project (say B) depends on the types declared in the other sub-project (say A), the compiler uses the .d.ts files from the sub-project A to typecheck and compile the sub-project B without the need for compiling the sub-project A again.当您的一个子项目(比如 B)依赖于在另一个子项目(比如 A)中声明的类型时,编译器使用子项目 A 中的.d.ts文件对子项目进行类型检查和编译B无需再次编译子项目A。 This results in faster compile times in large projects.这导致大型项目的编译时间更快。


declarationMap option declarationMap选项

When you enable the declarationMap option, the Typescript compiler creates the declaration source map ( .d.ts.map ) files.当您启用declarationMap选项时,Typescript 编译器会创建声明源 map ( .d.ts.map ) 文件。 The declaration source map files contain mapping definitions that link each type declaration generated in .d.ts files back to your original source file ( .ts ).声明源 map 文件包含将.d.ts文件中生成的每个类型声明链接回原始源文件 ( .ts ) 的映射定义。 The mapping definitions in these files are in JSON format.这些文件中的映射定义采用 JSON 格式。

These are used by your editor/IDE.这些由您的编辑器/IDE 使用。 You'll be able to use editor features like “Go to Definition” and Rename to navigate and edit the code across sub-projects.您将能够使用“转到定义”和重命名等编辑器功能来导航和编辑跨子项目的代码。 That means, for example, if you rename a type in one sub-project, the change will be propagated to the other sub-projects too.这意味着,例如,如果您重命名一个子项目中的类型,则更改也将传播到其他子项目。


Note that you can use these options even if you don't split your project.请注意,即使您不拆分项目,也可以使用这些选项。 This is useful if you want to have the editor feature “Go to Definition” for .d.ts files.如果您想为.d.ts文件提供编辑器功能“转到定义”,这很有用。 If you hit “Go to Definition” on a declaration from a .d.ts file, you will be taken to the source file ( .ts ) location instead of location of .d.ts .如果您在.d.ts文件的声明上点击“转到定义”,您将被带到源文件 ( .ts ) 位置,而不是.d.ts的位置。 But these options really shine in split projects.但是这些选项确实在拆分项目中大放异彩。

That's it.而已。 Hope that helps.希望有帮助。

If you have a project with more than 150 files or so, it's recommended to split it into smaller sub-projects using Project References .如果您的项目包含超过 150 个文件左右,建议使用Project References将其拆分为较小的子项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM