首页 > 解决方案 > TS2709:不能使用命名空间“InspectOptions”作为类型

问题描述

我目前正在编译一个打字稿文件,其中包含以下内容index.ts

export { default as Button } from './component/Button';
export type { Icon } from './component/Button';

我目前正在导出声明,Icon因为我将在另一个文件中重用它。

通过 lerna 命令编译它时lerna run build,我得到了这个:

../../node_modules/@types/node/console.d.ts:35:37 - error TS2709: Cannot use namespace 'InspectOptions' as a type.

35             dir(obj: any, options?: InspectOptions): void;
                                       ~~~~~~~~~~~~~~

../../node_modules/@types/node/repl.d.ts:101:43 - error TS2709: Cannot use namespace 'InspectOptions' as a type.

101     const writer: REPLWriter & { options: InspectOptions };
                                              ~~~~~~~~~~~~~~

当我尝试通过 编译它时tsc --project ./tsconfig.build.json,我得到了这个:

src/index.ts:2:1 - error TS1128: Declaration or statement expected.

2 export type { Icon } from './component/Button';
  ~~~~~~

src/index.ts:2:13 - error TS1005: ';' expected.

2 export type { Icon } from './component/Button';
              ~

src/index.ts:2:27 - error TS1005: ';' expected.

2 export type { Icon } from './component/Button';

如下tsconfig.build.json

{

  "compilerOptions": {
    "module": "esnext",
    "target": "es5",
    "outDir": "./dist",
    "jsx": "react",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "isolatedModules": false,
    "sourceMap": true,
    "resolveJsonModule": true,
    "moduleResolution": "node",
    "declaration": true,
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "dist"
  ],
}

我还在 github 上看到了这篇文章:https ://github.com/Microsoft/TypeScript/issues/27311但不确定如何应用提到的解决方案或是否适用于上述问题。

任何想法?

复制在Github上创建的问题的最小存储库

标签: reactjstypescript

解决方案


我已经通过运行解决了上述问题npm install -g typescript@latest

如果您想查看系统上安装的当前 typescript 版本,请运行tsc -v

感谢 Aluan 指出这一点。

如果在 typescript 更新后仍然遇到这个问题,可以考虑"skipLibCheck": true,tsconfig.json


推荐阅读