JavaScript is required for this page to work.
⤎ Back to blog overview

Adding global TypeScript types and interfaces to Nuxt projects

In some cases, making certain types and interfaces globally accessible (auto-imported) during development is necessary or preferred.

When I need global types in my Nuxt projects I create a /types/global.d.ts file and add my types and interfaces there.

interface LinksObject {
  count: number
  links: string[]
}
type StandardSize = 'l' | 'm' | 's'

Note that you do not add any export statements before the type or interface and there are no changes needed to the tsconfig.json file.

Using a global.d.ts file is however a trade-off. It can simplify certain aspects of type declaration but may introduce challenges in maintainability and scalability. Evaluate your project’s needs and consider whether the convenience of global declarations outweighs the potential downsides. For larger projects, modular declarations and proper type management are often preferred to maintain clean and maintainable code.