Auto-Generated Types
Nuxt projects rely on auto-generated types to work properly. These types are stored in the.nuxt directory and are generated when you run the dev server or build your application.
The generated tsconfig.json files inside the .nuxt directory include:
- Recommended TypeScript configuration for your project
- References to auto-imports for components and composables
- API route types for type-safe server routes
- Path aliases like
#imports,~/file, or#build/file - And more
Type-Checking
By default, Nuxt doesn’t check types when you runnuxt dev or nuxt build, for performance reasons. However, you can enable type-checking in several ways.
Installing Dependencies
To enable type-checking, first installvue-tsc and typescript as development dependencies:
Running Type Checks
You can run thenuxt typecheck command to check your types:
Type-Checking During Development
To enable type-checking at build or development time, use thetypescript.typeCheck option in your nuxt.config file:
nuxt.config.ts
Project References
Nuxt uses TypeScript project references to improve type-checking performance and provide better IDE support. When you runnuxt dev, nuxt build, or nuxt prepare, Nuxt generates multiple tsconfig.json files for different parts of your application:
.nuxt/tsconfig.app.json- Configuration for application code withinapp/directory.nuxt/tsconfig.node.json- Configuration fornuxt.config.tsand files outside other contexts.nuxt/tsconfig.server.json- Configuration for server-side code.nuxt/tsconfig.shared.json- For code shared between app and server contexts
Benefits
- Faster builds - TypeScript can skip rebuilding unchanged projects
- Better IDE performance - Faster IntelliSense and error checking
- Isolated compilation - Errors in one part don’t prevent compilation of others
- Clearer dependency management - Each project explicitly declares its dependencies
Augmenting Types
Since the project is divided into multiple type contexts, it’s important to augment types within the correct context:- For the
appcontext, place augmentation files in theapp/directory - For the
servercontext, place files in theserver/directory - For shared types, place files in the
shared/directory
app/types/custom.d.ts
Strict Checks
TypeScript comes with certain checks to give you more safety and analysis of your program. Strict checks are enabled by default in Nuxt whentypescript.typeCheck is enabled.
If you’re currently converting your codebase to TypeScript, you may want to temporarily disable strict checks by setting strict to false:
nuxt.config.ts
Type-Safe API Routes
When using API routes, Nuxt generates typings for these routes as long as you’re returning a value instead of usingres.end() to send a response.
You can access these types when using $fetch() or useFetch():
server/api/user.ts
app/app.vue
Type Utilities
Nuxt provides several type utilities to help you write type-safe code:PageMeta
Define metadata for your pages with full type safety:pages/index.vue
RouteMiddleware
Define type-safe route middleware:middleware/auth.ts
Plugin Context
Define type-safe plugins:plugins/my-plugin.ts
Best Practices
- Enable type-checking - Catch errors early in development
- Use auto-generated types - Run
nuxt prepareto generate types - Leverage IntelliSense - Your IDE will provide better autocomplete
- Return values from API routes - Enable type inference
- Use
definePageMeta- Get type-safe page metadata - Keep TypeScript updated - Benefit from the latest features and improvements
Common Patterns
Type-Safe Environment Variables
nuxt.config.ts
Type-Safe State
composables/useUser.ts