在导入类型时不使用 type 关键字


🌐 Importing types without type keyword

由于类型剥离的特性,type 关键字对于正确剥离类型导入是必要的。没有 type 关键字,Node.js 会将导入视为值导入,这将导致运行时错误。可以使用 tsconfig 选项 verbatimModuleSyntax 来匹配此行为。

🌐 Due to the nature of type stripping, the type keyword is necessary to correctly strip type imports. Without the type keyword, Node.js will treat the import as a value import, which will result in a runtime error. The tsconfig option verbatimModuleSyntax can be used to match this behavior.

此示例将正常工作:

🌐 This example will work correctly:

import type { Type1, Type2 } from './module.ts';
import { fn, type FnParams } from './fn.ts'; 

这将导致运行时错误:

🌐 This will result in a runtime error:

import { Type1, Type2 } from './module.ts';
import { fn, FnParams } from './fn.ts';