I'm using tsconfig to check my js files. I have my custom type definitions in the custom_types\custom.d.ts
tsconfig.json
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"allowJs": true,
"checkJs": true,
"outDir": "./dist",
"strict": true,
"typeRoots": ["./custom_types/", "node_modules/@types/"],
"types": ["node"],
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"]
}
custom_types/custom.d.ts
declare global {
const foo: string;
namespace NodeJS {
interface Global {
foo: string;
}
}
}
export default global;
src/app.js
let bar = foo; // Cannot find name 'foo'.ts(2304)
However when I do this in my tsconfig:
{
...
},
"include": ["src/**/*", "custom_types/custom.d.ts"]
}
everything work perfect.
So this makes me think that my tsconfig is wrong and TypesScript completely ignores this part:
"typeRoots": ["./custom_types/", "node_modules/@types/"],
What am I'm doing wrong when trying to specify path to the folder with my own custom type definitions?
p.s. "node_modules/@types/" is picked up by TypeScript properly (everything that is inside @types is available to me). However "./custom_types/" is ignored.
UPD:
Many answers out there are suggestion to do something with baseUrl and paths. However doing this did nothing for me:
"baseUrl": "./",
"paths": {
"@custom_types": ["*", "./custom_types/*"]
},
"typeRoots": ["@custom_types/custom.d.ts", "node_modules/@types/"],