In your Next.js TS project, you can store your custom type definitions (.d.tsΒ files) in theΒ typesΒ directory at the root level of your project (Convention)
my-nextjs-app/
βββ components/
βββ pages/
βββ styles/
βββ types/
β βββ my-module.d.ts
βββ utils/
βββ next.config.js
βββ package.json
βββ tsconfig.json
βββ yarn.lock`
TheΒ my-module.d.tsΒ file contains the type definitions for the my-module module you are using in your project.
Inform TypeScript of your custom types
To ensure that TypeScript can find and use your custom type definitions, you need to configure theΒ typeRootsΒ option in yourΒ tsconfig.jsonΒ file. Update theΒ tsconfig.jsonΒ file as follows:
{
"compilerOptions": {
"typeRoots": ["./types", "./node_modules/@types"]
},
// Other configuration options...
}By addingΒ "./types"Β to theΒ typeRootsΒ array, TypeScript will look for type definitions in theΒ typesΒ directory relative to your projectβs root.