本文介绍了我是否需要使用“导入类型"TypeScript 3.8的功能是否全部导入都来自我自己的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的文件 types.ts ,其中定义了一些类型:

 导出接口MyInterface {//...}导出const枚举MyEnum {//...}导出类型MyType = {//...} 

我已阅读有关最新打字稿此处.据我了解,这是为了修复特定问题,这些问题似乎大部分是从.js文件导入时发生的.

我可以使用 import import type 语句导入我的类型.两者似乎都可以正常工作.问题是我应该更喜欢 import type 还是为了更明确地帮助我避免一些理论上的极端情况问题,还是我可以简单地使用 import 并依靠 import elision 将其从已编译的代码中删除?

换句话说:在这里使用 import type 有什么好处,还是应该针对特定情况使用它来解决 import elision 的缺点?

解决方案

简短答案:通过使用 import type export type 声明似乎可以通过防止边缘情况下的问题以及通过使用类型定义分析为当前和即将推出的工具提供更好的基础来改善处理性能和可靠性的方式来产生显着的收益.

长答案:

TypeScript 3.8发行说明说:

以下是两个实际的示例,这些残余的导入如何导致构建或运行时错误:

另一个好处与分析类型定义的工具有关.目前,有关于使用Babel进行捆绑程序设置的好处的详细信息,但目前或以后可能还会使其他工具(例如IDE性能)受益.

对于Babel用户,手动配置其设置:如果您使用的是Babel 7.9 =>在您使用TS 3.8 =>的捆绑程序设置中,则可以删除以前需要的 @ babel/plugin-transform-typescript 插件.

对于使用预构建的Babel预设的设置:Babel的团队建议配置Babel预设,以便使用显式的仅类型导入.

在博客文章中了解更多信息: Babel 7.9减少捆绑大小,添加TypeScript 3.8支持.

有关在TypeScript中使用Babel 的更多相关信息TS文档.

详细了解使用TS编译器选项以及 isolatedModules 选项的好处仅类型导入-一种新的TypeScript功能,使Babel用户受益

I have a simple file types.ts that defines some types:

export interface MyInterface {
   // ...
}

export const enum MyEnum {
   // ...
}

export type MyType = {
  // ...
}

I have read about the new feature import type for the latest typescript here. As far as I understand it is meant to fix specific problems which seems mostly to happen when importing from .js files.

I can import my types with both import and import type statements. Both seems to work equally fine. The question is should I prefer import type for being more explicit and helping me to avoid some theoretical edge-case problems or can I just use import for simplicity and rely on import elision to remove these from compiled code?

In other words: is there any benefit of using import type here or it should rather be used for specific cases to work around import elision shortcomings?

解决方案

Short answer: Being more explicit by using import type and export type statements seem to yield explicable benefits by safeguarding against edge-case problems, as well as giving current and upcoming tooling better ground for improving processing performance and reliability with type definition analysis.

Long answer:

As TypeScript 3.8 release notes say:

Here are two practical examples how these remnant imports can cause errors in build or runtime:

Another benefit relates to tooling that is analyzing type definitions. Currently there are details about benefits with bundler setups using Babel, but may currently or later benefit other tooling as well (like IDE performance).

For Babel users manually configuring their setup: If you are using Babel 7.9=> in your bundler setup with TS 3.8=>, then you can possibly remove the previously needed @babel/plugin-transform-typescript plugin.

For those setups that are using pre-built Babel presets: Babel's team is recommending of configuring Babel presets so that explicit type-only imports are to be used.

Read more in a blog post: Babel 7.9 Reduces Bundle Sizes, Adds TypeScript 3.8 Support.

More of relevant info about Using Babel with TypeScript in TS docs.

Detailed look into benefits using and how isolatedModules TS compiler option works type-only imports — A new TypeScript feature that benefits Babel users

这篇关于我是否需要使用“导入类型"TypeScript 3.8的功能是否全部导入都来自我自己的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:51