tsconfig.json doesn't care about allowJs

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?

Upvotes: 11

Views: 31453

Answers (3)

NeoZoom.lua
NeoZoom.lua

Reputation: 2921

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.

Upvotes: 1

YinPeng.Wei
YinPeng.Wei

Reputation: 598

{
  "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).

Upvotes: 10

mth khaled
mth khaled

Reputation: 333

"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

Upvotes: -1

Related Questions