Typescript 编译器错误 TS1005:预期为“,”

use*_*805 1 javascript typescript babeljs

我收到打字稿编译器错误。

[tsl] ERROR in /.../node_modules/@remirror/react-components/node_modules/@mui/base/useMenu/useMenu.d.ts(3,15)
      TS1005: ',' expected.
ts-loader-default_e3b0c44298fc1c14
Run Code Online (Sandbox Code Playgroud)

在该文件中,导入导致了错误:

import { type MenuUnstyledContextType } from '../MenuUnstyled';

'type' is declared but its value is never read.ts(6133)
Module '"../MenuUnstyled"' has no exported member 'type'. Did you mean to use 'import type from "../MenuUnstyled"' instead?ts(2614)
Run Code Online (Sandbox Code Playgroud)

不过,这在 Typescript 3.8 中似乎是有效的,并且该应用程序使用typescript ^4.3.5@babel/preset-typescript ^7.14.5

https://www.typescriptlang.org/docs/handbook/modules.html#:~:text=With%20TypeScript%203.8%2C%20you%20can,statement%2C%20or%20using%20import%20type%20

// Explicitly pull out a value (getResponse) and a type (APIResponseType) 
import { getResponse, type APIResponseType} from "./api";
Run Code Online (Sandbox Code Playgroud)

有什么想法如何解决吗?

Ber*_*rgi 9

\n

这在 Typescript 3.8 中似乎是有效的

\n
\n

不幸的是,事实并非如此 -您发现的文档非常具有误导性。

\n

TypeScript 3.8引入了仅类型导入,语法如下import type \xe2\x80\xa6

\n
\n

作为 TypeScript 3.8 中的解决方案,我们\xe2\x80\x99 添加了用于仅类型导入和导出的新语法。

\n
从 "./some-module.js" 导入类型 { SomeThing };\n\n导出类型 { SomeThing };
\n

import type仅导入用于类型注释和声明的声明。它总是被完全擦除,因此在运行时\xe2\x80\x99s 没有任何残留。同样,export type仅提供可用于类型上下文的导出,并且也会从 TypeScript\xe2\x80\x99s 输出中删除。

\n
\n

您正在寻找的语法是在 TypeScript 4.5 中引入的

\n
\n

type导入名称的修饰符

\n

[类型导入]有效,但最好避免同一模块有两个导入语句。这就是为什么 TypeScript 4.5 允许type对单个命名导入进行修饰符的部分原因,以便您可以根据需要进行混合和匹配。

\n
import { someFunc, type BaseType } from "./some-module.js";\nexport class Thing 实现 BaseType {\n someMethod() {\n someFunc();\n }\n}
\n
\n

因此,请更新您的 TypeScript 版本,或将导入声明更改为

\n
import type { APIResponseType } from "./api";\nimport { getResponse } from "./api";\n
Run Code Online (Sandbox Code Playgroud)\n