【问题标题】:Typescript export named default of module打字稿导出命名为模块的默认值
【发布时间】:2020-04-13 09:22:04
【问题描述】:

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

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

并且 index.js 将包含:

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

我相信 babel 转换为:

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

我已经将所有 .js/.jsx 文件转换为 .ts/.tsx 并且我正在使用带有 babel-loader 的 webpack 来编译这些文件。 Everyting 运行得很好,但我在编辑器中遇到 eslint 错误,因为 typescript 不理解这些导出。这是我的 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

【问题讨论】:

  • 这能回答你的问题吗? TypeScript export imported interface
  • 我知道我可以更改代码以使其与打字稿兼容,但我想知道是否有办法修改我的 tsconfig 以了解我的导出方法。
  • 打字稿不支持export Thing from './place'。你能得到的最接近的是export { default as Thing } from './place'
  • export default from 语法提案目前处于第 1 阶段,因此在 TS 中尚不可用。

标签: javascript reactjs typescript


【解决方案1】:

正如 cmets 中所述,这似乎仍是第 1 阶段的功能,尚未在 Typescript 中实现。

【讨论】:

    猜你喜欢
    • 2017-09-23
    • 2021-11-24
    • 2019-12-11
    • 2017-02-06
    • 2016-01-23
    • 2016-02-10
    • 2021-02-07
    • 2019-09-07
    • 2018-04-17
    相关资源
    最近更新 更多