Skip to content
开发文档
Migrating from Tsc

从 tsc 迁移

¥Migrating from tsc

如果你要从 TypeScript Compiler(tsc) 迁移,请记住以下几点。

¥If you are Migrating from TypeScript Compiler(tsc), there are a few things to keep in mind.

TypeScript 版本

¥TypeScript version

SWC 支持最新的稳定 TypeScript。

¥SWC supports the latest stable TypeScript.

isolatedModules:true

SWC 逐个文件地工作,因此任何依赖于理解完整类型系统的代码转换都将不起作用。

¥SWC works on file-by-file, so any code transforms that depend on understanding the full type system will not work.

如果遇到这些限制,某些 TypeScript 功能(例如常量枚举和命名空间)可能会导致运行时问题。

¥If you encounter these limitations, certain TypeScript features such as const enums and namespaces may cause runtime problems.

在这种情况下,在 TypeScript 中使用 isolatedModules (opens in a new tab) 标志可以帮助警告你 SWC 可能无法正确解释的任何代码。

¥In this case, using the isolatedModules (opens in a new tab) flag in TypeScript can help to warn you of any code that may not be correctly interpreted by SWC.

详细信息请参见 对相关问题的注释 (opens in a new tab)

¥See a comment on the related issue (opens in a new tab) for more details.

importsNotUsedAsValues:"error"

由于上述原因,SWC 无法完全辨别导入的绑定是 value 还是 type

¥Due to the aforementioned reasons, SWC is unable to completely discern whether the imported binding is a value or a type.

将此 importsNotUsedAsValues (opens in a new tab) 选项设置为 error 将确保 TypeScript 在类型检查期间正确地将所有类型导入标记为 type,从而在 SWC 中准确地删除它们。

¥Setting this importsNotUsedAsValues (opens in a new tab) option to error will ensure that TypeScript properly marks all type imports during type checking as type, thus removing them accurately in SWC.

esModuleInterop:true

TypeScript 的导入互操作性偏离了 ES6 模块规范。

¥The TypeScript's import interoperability deviates from the ES6 modules specification.

另一方面,SWC 采用与 Babel 类似的方法(有时可能更严格)。

¥SWC, on the other hand, adopts a similar approach to Babel (which can sometimes be more stringent).

启用此 esModuleInterop (opens in a new tab) 选项可确保 tsc 的行为与 SWC 的行为一致。

¥Enabling this esModuleInterop (opens in a new tab) option ensures that tsc's behavior aligns with that of SWC.

verbatimModuleSyntax:true

这是 TypeScript 5.0 中引入的新选项,用于替换 isolatedModulespreserveValueImportsimportsNotUsedAsValues。请检查 发行公告 (opens in a new tab) 了解更多详情。

¥This is a new option introduced in TypeScript 5.0 to replace isolatedModules, preserveValueImports and importsNotUsedAsValues. Please check the release note (opens in a new tab) for further details.

useDefineForClassFields

这个问题涉及到 [[Define]][[Set]] 的语义。

¥This issue involves the semantics of [[Define]] and [[Set]].

谁不需要照顾呢?

¥Who does not need to take care of it?

  • 那些从不使用类的人。

    ¥Those who never use classes.

  • 那些使用类但从不使用继承的人。

    ¥Those who use classes but never use inheritance.

哪些人需要特别注意这个问题?

¥Who needs to pay special attention to this matter?

  • 装饰器用户。

    ¥Decorator users.

如果该值已在 tsconfig.json 中设置,则可以在 swc 的配置中使用相同的值。

¥If the value has already been set in your tsconfig.json, then the same value can be used in the configuration of swc.

如果没有设置而你遇到了问题,那么你就需要补充这个设置了。

¥If it has not been set and you encounter a problem, then it is necessary for you to supplement this setting.

需要注意的是,这个选项的默认值会根据你的 tsconfig.jsontarget 而改变。

¥It should be noted that the default value of this option will change depending on the target of your tsconfig.json.

请检查 useDefineForClassFields (opens in a new tab) 选项。

¥Please check the useDefineForClassFields (opens in a new tab) option.

如果目标是 ES2022 或更高版本(包括 ESNext),则为 true,否则为 false。

¥true if target is ES2022 or higher, including ESNext, false otherwise.

已知的问题

¥Known issues

  • TypeScript#16166 (opens in a new tab) ES6 导入不会被 tsc 吊起。如果你依赖错误的 tsc 实现,则在迁移到 swc 时可能会遇到问题,因为 swc 更严格地保留 ES 模块语义。

    ¥TypeScript#16166 (opens in a new tab) ES6 imports are not hoisted by tsc. If you rely on erroneous tsc implementation, you may encounter issues when migrating to swc, as swc more rigorously preserves ES module semantics.

注意

¥Notes

SWC 仅转换代码,不执行类型检查。因此,建议你继续使用 tsc 来检测任何类型错误。

¥SWC only transpiles the code and doesn't perform type checking. Therefore, it's recommended that you continue to use tsc for detecting any type errors.

Last updated on November 24, 2023