Despite attempting various methods recommended in other sources, I am struggling to configure a TypeScript project that utilizes an untyped NPM module. Below is a simplified example along with the steps I have taken.
Let's imagine, for this demonstration, that lodash
lacks predefined type definitions. Consequently, we will disregard the @types/lodash
package and endeavor to manually introduce its typings file lodash.d.ts
into our project.
Directory structure
- node_modules
- lodash
- src
- foo.ts
- typings
- custom
- lodash.d.ts
- global
- index.d.ts
- custom
- package.json
- tsconfig.json
- typings.json
Now, let's delve into the files.
File foo.ts
///<reference path="../typings/custom/lodash.d.ts" />
import * as lodash from 'lodash';
console.log('Weeee');
The content of File lodash.d.ts
has been directly copied from the original @types/lodash
package.
File index.d.ts
/// <reference path="custom/lodash.d.ts" />
/// <reference path="globals/lodash/index.d.ts" />
File package.json
{
"name": "ts",
"version": "1.0.0",
"description": "",
"main": "index.js",
"typings": "./typings/index.d.ts",
"dependencies": {
"lodash": "^4.16.4"
},
"author": "",
"license": "ISC"
}
File tsconfig.json
{
"compilerOptions": {
"target": "ES6",
"jsx": "react",
"module": "commonjs",
"sourceMap": true,
"noImplicitAny": true,
"experimentalDecorators": true,
"typeRoots" : ["./typings"],
"types": ["lodash"]
},
"include": [
"typings/**/*",
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
File typings.json
{
"name": "TestName",
"version": false,
"globalDependencies": {
"lodash": "file:typings/custom/lodash.d.ts"
}
}
Despite trying multiple approaches to include typings:
- Directly importing it in
foo.ts
- Using a
typings
property inpackage.json
- Utilizing
typeRoots
intsconfig.json
with a filetypings/index.d.ts
- Employing an explicit
types
declaration intsconfig.json
- Including the
types
directory intsconfig.json
- Creating a custom
typings.json
file and executingtypings install
Nevertheless, when compiling Typescript:
E:\temp\ts>tsc
error TS2688: Cannot find type definition file for 'lodash'.
What could be the issue here?