导出类型的打字稿问题

时间:2020-08-11 11:46:44

标签: typescript

我尝试编译入口TS文件,并发生以下问题:

> 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';
                            ~~~~~~~~~~~~~~~~~~~~

index.ts文件的内容如下:

export { default as Button, Size, ButtonStyle, colors } from './component/Button';
export type { Icon } from './component/Button';

tsconfig.build.json文件的内容如下:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "sourceMap": true,
    "resolveJsonModule": true,
    "declaration": true,
    "outDir": "./dist",
    "jsx": "react",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "isolatedModules": false,
  },
  "include": [
    "src/**/*"
  ]
}

对于export type {xxx}中没有index.ts的文件,它们已成功编译。而且,如果我从该文件中删除了export type { Icon } from './component/Button',该问题将消失。但是,我需要将这些对象放在另一个文件中。

有什么主意吗?

1 个答案:

答案 0 :(得分:0)

请尝试将"module": "commonjs"更改为"module": "esnext",

commonjs不支持导出/导入语法。

请在此处查看更多详细信息: https://flaviocopes.com/commonjs/