【问题标题】:module.exports in typescript file return 'module is not a module.'打字稿文件中的 module.exports 返回“模块不是模块。”
【发布时间】:2020-01-22 21:29:41
【问题描述】:

在JS文件中编写的以下模块:

module.exports = {
  propA: 1,
  propB: 2
}

允许稍后从模块中导入属性,例如: import { propA } from 'path/to/module'

但是,将文件格式更改为 typescript(如 module.ts)会导致以下问题 linter issue 'module is not a module.'

这是项目的 tsconfig

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "allowJs": true,
    "sourceMap": true,
    "noImplicitThis": true,
    "baseUrl": ".",
    "types": [
      "webpack-env",
      "jest"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

如果有人能提出一种有效的方法来保存 module.exports 或 typescript ,我将不胜感激:)

...或者至少可以声明导出对象的模块,以后只允许导入它的单个属性

【问题讨论】:

标签: typescript


【解决方案1】:

试试这样:

export const yourData = {
  propA: 1,
  propB: 2
}

并从“yourSrc”导入 yourData

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 2020-06-28
    • 2015-11-18
    • 2017-03-11
    • 1970-01-01
    • 2023-03-29
    • 2017-12-10
    相关资源
    最近更新 更多