I normally setup my tsconfig.json
with strict
set to true
. This imply that noImplicitAny
is also set to true
. However, when strict
is set, typescript seems to ignore my typeRoots
entry for locally created *.d.ts
file. Here is a sample tsconfig.json
that I use:
{
"compilerOptions": {
"declaration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2017" ],
"module": "CommonJS",
"noImplicitReturns": true,
"outDir": "lib",
"removeComments": true,
"sourceMap": true,
"strict": true,
"target": "es2017",
"typeRoots": [ "./typings", "./node_modules/@types" ],
"types": [ "node" ],
},
"compileOnSave": true,
"include": [ "./src/**/*" ]
}
I can add the following to tsconfig.json
above and it will work:
{
"compilerOptions": {
...
"noImplicitAny": false,
...
}
}
Here is a sample project I created to illustrate this problem:
https://github.com/marcoslin/tstyping-test
Any idea why this is happening?
noImplicitAny: false
does not really fix the problem it just ignores it. template
will be implicitly typed as any
as no types are found. You get no errors but no type safety either.
The real problem is that you specify "types": [ "node" ],
this means that only the types for the node module are taken from typeRoots
. See docs.
The simplest solution is to remove the types
element from tsconfig.json
. This tsconfig does not give any errors:
{
"compilerOptions": {
"declaration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2017" ],
"module": "CommonJS",
"noImplicitReturns": true,
"outDir": "lib",
"removeComments": true,
"sourceMap": true,
"strict": true,
"target": "es2017",
"typeRoots": [ "./typings", "./node_modules/@types" ]
},
"compileOnSave": true,
"include": [ "./src/**/*" ]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With