I'm in the process of incorporating TypeScript into an existing JavaScript project, and I'm currently facing the challenge of adding typings to an npm module that lacks type definitions, which I anticipate will be a common issue.
When I try to import the module using
import X from 'foo';
I encounter an error stating Cannot find module 'foo'
Within my typings/modules
directory, I have created a foo
folder containing an index.d.ts file with the following content:
declare module foo {
export default class XXX{
}
}
In the main typings/index.d.ts
file, I've included
/// <reference path="modules/foo/index.d.ts" />
Yet, the problem persists.
Here is a snippet of my tsconfig.json configuration file:
{
"compilerOptions": {
"target": "esnext",
"baseUrl": "./src",
"jsx": "react",
"allowSyntheticDefaultImports": true,
"allowJs": true,
"moduleResolution": "node",
"module": "es2015",
"experimentalDecorators": true,
"lib": ["es2015", "dom"],
"typeRoots" : ["./typings/modules", "./node_modules/@types"]
},
"include": [
"./src/**/*.ts",
"./src/**/*.tsx"
]
}