最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - tsconfig.json doesn't care about allowJs - Stack Overflow

programmeradmin1浏览0评论

I am trying to use in my Angular 5 application a .js file which is avaible just in JavaScript language, so I can't find an alternative in TypeScript.

The problem is that I have added the "allowJs": true in my tsconfig.json, but the error is still active. My tsconfig.json is:

{
  "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"
    ]
  }
}

The error is:

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

Whats wrong?

I am trying to use in my Angular 5 application a .js file which is avaible just in JavaScript language, so I can't find an alternative in TypeScript.

The problem is that I have added the "allowJs": true in my tsconfig.json, but the error is still active. My tsconfig.json is:

{
  "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"
    ]
  }
}

The error is:

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

Whats wrong?

Share Improve this question edited Jan 21, 2018 at 21:59 Carlos Vázquez Losada asked Jan 21, 2018 at 21:35 Carlos Vázquez LosadaCarlos Vázquez Losada 9501 gold badge17 silver badges36 bronze badges 4
  • You can try to add checkJs: false to the config. And its better to post the error message. – alexKhymenko Commented Jan 21, 2018 at 21:42
  • Error added. checkJs: false doesn't solve my problem – Carlos Vázquez Losada Commented Jan 21, 2018 at 22:00
  • Can you rename that file to .ts? Keep in mind that any valid js file is a valid ts file as well, so changing the extension shouldn't cause any trouble (other than the usual type warnings perhaps) and should get rid of the error. – Arash Motamedi Commented Jan 21, 2018 at 23:00
  • I have js file generate with prisma generate client it is in my project but not on git, it is necessary since I use yarn4 that it is within the scope that I can import. I also have the same issue when using allowJs: true it can be imported, but checkJs: false does not work and I don't know what to do because I can't use annotation in generated file. It suck – Dimitri Kopriwa Commented Jul 4, 2024 at 20:52
Add a comment  | 

3 Answers 3

Reset to default 10
{
  "compilerOptions": {
    "outDir": "./built",
    "allowJs": true,
  }
}

You can only run tsc to compile your js file.

if u run tsc file.js, it will throw an error.

Because if you run tsc, it will find the nearest config file, but if you run tsc file.js, it will run depending on the default file(allowJs:false, ignore tsconfig.json).

allowJS: Allow JavaScript files to be imported inside your project, instead of just .ts and .tsx files. (Please take a look into the official docs, which should be available during 2022)

The problem is that I have added the "allowJs": true in my tsconfig.json, but the error is still active.

That means your tsconfig.json is not referenced during compilation.

"AllowJs" option allows you To accept js files as input for ts files.

You can migrate your js file to typescript, follow this link for more details: https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html

发布评论

评论列表(0)

  1. 暂无评论