网站搜索

从类型定义导入类型时的 TypeScript TS1005 错误


我的项目使用的其中一个库(parse5)在 .d.ts 文件中使用以下语法来导入类型,暴露了类型信息:

import { type CharacterToken, type DoctypeToken, type TagToken, type EOFToken, type CommentToken } from '../common/token.js';

这在运行应用程序时导致了很多 TypeScript 错误,因为看起来 TypeScript 不识别这种导入类型的方式。

如果我更改语法为

import type { CharacterToken, DoctypeToken, TagToken, EOFToken, CommentToken } from '../common/token.js';

我没有收到任何错误。

会不会是我使用了错误的 tsc 版本?

答案#1

'parse5' 包中用于导入类型的语法是在 TypeScript 4.5 中引入的:https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#type-modifiers-on-import-names

我在一个使用 TypeScript 4.4 的项目中遇到了同样的问题,升级到 4.5 解决了我的问题。

检查 parse5 上的未解决问题显示,许多人有同样的问题,例如,看看 https://github.com/inikulin/parse5/issues/913#issuecomment-1520222908

答案#2

在 TypeScript(3.8) 中,你应该使用 import type { SomeThing } from "./some-module.js";,而不是 import { type SomeThing } from "./some-module.js";

更多信息