I'm using JS library which does not provide @type definition for TypeScript, so I created my own .d.ts file. Let's call it foo.d.ts. My project structure looks like this:
...
.next/
pages/
...
typings/
foo.d.ts
...
tsconfig.json
My VS Code has no problem with that definition and I can do import in my component, like:
import {Foo} from "foo";
But in run time I get this error in the browser console
Module not found: Can't resolve 'foo'
I have tried to add
"typeRoots": [ "node_modules/@types", "typings" ]
to my tsconfig.json but it didn't help. I've also tried to explicitly add foo.d.ts to the include section where next-env.d.ts is added.
foo.d.ts looks like this:
declare module 'foo' {
declare interface ValidationError {
message?: string;
}
declare namespace Foo {
class Bar {
constructor(config: any, displayErrorsCallback: (data: ValidationError[]) => void, onSubmitTriggered: () => void);
}
}
}
Update (added tsconfig)
{
"compilerOptions": {
"target": "es6",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"typeRoots": [
"node_modules/@types",
"typings"
]
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}
foo? .d.ts files just declare types and assume the package is present, you still have tonpm installthe package itself.tsconfig.jsonplease?