打字稿导出命名为模块的默认



我刚刚将我的 React 项目从 javascript 转换为打字稿,我有很多实例,我将在目录中有一个索引文件并从相应的目录中导出所有函数/组件。例如,目录可能如下所示:

components
|--index.js
|--ComponentA
|--ComponentB
|--ComponentC
|...

索引.js将包含:

export ComponentA from './ComponentA'
export ComponentB from './ComponentB'
export ComponentC from './ComponentC'
...

我相信巴别塔可以转换为:

export { default as ComponentA } from './ComponentA'
...

我已经将所有.js/.jsx文件转换为.ts/.tsx,并且我正在使用带有babel-loader的webpack来编译这些文件。每次运行都很好,但我在编辑器中遇到 eslint 错误,因为打字稿不理解这些导出。这是我的tsconfig.json文件:

{
"compilerOptions": {
"baseUrl": ".",
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": false,
"module": "es6",
"target": "es6",
"jsx": "preserve",
"lib": ["es5", "es6", "dom"],
"esModuleInterop": true,
"allowJs": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true
},
"include": [
"./src/**/*"
],
"types": [
"@reachify/graphql-schema"
],
"awesomeTypescriptLoaderOptions": {
"reportFiles": [
"./src/**/*"
]
}
}

和.eslintrc.json:

{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"env": {
"browser": true
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2019,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint/eslint-plugin"
]
}

如何获取打字稿以理解export Component from './Component

正如评论中提到的,看起来这仍然是阶段 1 的功能,尚未在 Typescript 中实现。

最新更新