I'm in the process of migrating my JS codebase to TS. It uses the method Math.sign()
. However, the compiler gives this error-
Property 'sign' does not exist on type 'Math'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.
Which it shouldn't give since my target version is "es2016", and this was added in "es2015"
This is my tsconfig file currently, so I don't understand why this is happening and how to resolve this issue-
{
"compilerOptions": {
"target": "es2016",
"lib": ["es2016", "dom"],
// other things
}
}
When you pass the typescript compiler an input file, it ignore tsconfig.json
and will generate a new temporary config file containing the arguments you passed. You can check this by running
$ npx tsc index.ts --showConfig
{
"compilerOptions": {},
"files": [
"./index.ts"
]
}
To use your config file, you should place your input file(s) in the files
attribute like so
{
"compilerOptions": {
"target": "es2016",
"lib": ["es2016", "dom"],
// other things
},
"files": ["./index.ts"]
}
and run it like so
npx tsc
or optionally provide a path to your project
npx tsc --project path/to/folder/containing/tsconfig
If you're curious it's also possible to do this while avoiding a config file like so:
npx tsc index.ts --target es2016 --lib es2016,dom
You can indeed verify this works with --showConfig
again
$ npx tsc index.ts --target es2016 --lib es2016,dom --showConfig
{
"compilerOptions": {
"target": "es2016",
"lib": [
"es7",
"dom"
],
"module": "es6",
"moduleResolution": "classic"
},
"files": [
"./index.ts"
]
}