【问题标题】:tsconfig.json doesn't care about allowJstsconfig.json 不关心allowJs
【发布时间】:2018-01-21 21:35:13
【问题描述】:

我试图在我的 Angular 5 应用程序中使用一个 .js 文件,该文件仅在 JavaScript 语言中可用,所以我在 TypeScript 中找不到替代方案。

问题是我在 tsconfig.json 中添加了"allowJs": true,但错误仍然存​​在。我的 tsconfig.json 是:

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "allowJs": true,
    "baseUrl": "src",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

错误是:

'<path>/file.js' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.

怎么了?

【问题讨论】:

  • 您可以尝试将 checkJs: false 添加到配置中。最好发布错误消息。
  • 添加了错误。 checkJs: false 不能解决我的问题
  • 你能把那个文件重命名为.ts吗?请记住,任何有效的js 文件也是有效的ts 文件,因此更改扩展名不会造成任何问题(可能除了通常的类型警告)并且应该消除错误。

标签: javascript angular tsconfig


【解决方案1】:
{
  "compilerOptions": {
    "outDir": "./built",
    "allowJs": true,
  }
}

你只能运行tsc来编译你的js文件。

如果你运行tsc file.js,它会抛出一个错误。

因为如果你运行tsc,它会找到最近的配置文件,但如果你运行tsc file.js,它会根据默认文件运行(allowJs:false,忽略tsconfig.json)。

【讨论】:

    【解决方案2】:

    “AllowJs”选项允许您接受 js 文件作为 ts 文件的输入。

    您可以将您的 js 文件迁移到 typescript,点击此链接了解更多详情: https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html

    【讨论】:

      【解决方案3】:

      allowJS:允许在项目中导入 JavaScript 文件,而不仅仅是 .ts.tsx 文件。 (请查看official docs,应该在 2022 年可用)

      问题是我在我的 tsconfig.json 中添加了“allowJs”:true,但错误仍然存​​在。

      这意味着您的 tsconfig.json 在编译期间没有被引用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-12
        • 2017-07-07
        • 2016-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-23
        • 2023-03-28
        相关资源
        最近更新 更多