I have encountered a problem with my JS library and its declaration files (*.d.ts) in my TypeScript projects. For some reason, my TS project seems to be ignoring these declaration files.
To investigate this issue further, I decided to conduct a simple test within my TS project.
// project/src/calculateSum.js
function calculateSum(a, b) {
return a + b
}
module.exports = {
calculateSum
}
// project/src/types/custom/calculateSum.d.ts
export declare function calculateSum(a: number, b: number): number;
// project/src/app.ts
import { calculateSum } from './calculateSum`
console.log(calculateSum('it should not acceept string', 1))
Surprisingly, neither VS code nor TS reported any issues with the code above.
Below is a snippet of my tsconfig.json:
{
"compilerOptions": {
...
"skipLibCheck": true,
"typeRoots": [
"node_modules/@types",
"src/types/custom"
]
},
"include": [
"./src/**/*",
],
"exclude": [
"node_modules"
]
}
In addition, I also have an svg.d.ts file within src/types/custom, and surprisingly, it works as expected.
// project/src/types/custom/svg.d.ts
declare module "*.svg" {
import React = require("react")
export const AsComponent: React.FC<React.SVGProps<SVGSVGElement>>
const src: string
export default src
}
It appears that there might be something I am misunderstanding in this scenario.