I have encountered a situation where the npm package update-immutable
does not come with a built-in typescript definition or a definition in @types
. To resolve this issue, I created a type definition file within my project. As a result, VS Code is now able to detect the correct types for this package.
However, when the noImplicitAny
option is set to true, Angular's compiler fails to recognize this type definition file. This leads to an error message stating
Could not find a declaration file for module 'update-immutable'. '.../node_modules/update-immutable/dist/update.js' implicitly has an 'any' type.
How can I make sure that Angular's compiler locates the .d.ts
type definition file that I have created for the npm package being used?
After enabling the traceResolution
feature, I discovered that placing the file update-immutable.d.ts
directly under the project root allows it to be detected. However, I prefer to maintain organization and avoid cluttering the project root with type files if possible.
Project structure:
projectroot
+ src
| + app
| | + states
| | + core
| | + core.reducer.ts (where update-immutable is imported)
| + types
| + update-immutable
| + index.d.ts (custom type file)
+ node_modules
| + update-immutable
| + ...
+ tsconfig.json
tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"downlevelIteration": true,
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "esnext",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es2015",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
],
"paths": {
"jszip": [
"node_modules/jszip/dist/jszip.min.js"
]
},
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
index.d.ts
type definition:
declare module 'update-immutable' {
export default function update<T>(view: T, upd: object): T;
}