By default, Nuxt doesn't check types when you run nuxt dev or nuxt build, for performance reasons.
To enable type-checking at build or development time, install vue-tsc and typescript as development dependency:
npm install --save-dev vue-tsc typescript
yarn add --dev vue-tsc typescript
pnpm add -D vue-tsc typescript
bun add -D vue-tsc typescript
Then, run nuxt typecheck command to check your types:
npx nuxt typecheck
To enable type-checking at build or development time, you can also use the typescript.typeCheck option in your nuxt.config file:
export default defineNuxtConfig({
typescript: {
typeCheck: true,
},
})
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. You can also generate these files manually by running nuxt prepare.
The generated tsconfig.json files inside the .nuxt directory include recommended basic TypeScript configuration for your project, references to auto-imports, API route types, path aliases like #imports, ~/file, or #build/file, and more.
tsconfig.json file directly, as doing so could overwrite important settings. Instead, extend it via nuxt.config.ts. Learn more about extending the configuration here.Nuxt uses TypeScript project references to improve type-checking performance and provide better IDE support. This feature allows TypeScript to break up your codebase into smaller, more manageable pieces.
When you run nuxt dev, nuxt build or nuxt prepare, Nuxt will generate multiple tsconfig.json files for different parts of your application.
.nuxt/tsconfig.app.json - Configuration for your application code within the app/ directory.nuxt/tsconfig.node.json - Configuration for your nuxt.config.ts and files outside the other contexts.nuxt/tsconfig.server.json - Configuration for server-side code (when applicable).nuxt/tsconfig.shared.json - For code shared between app and server contexts (like types and non-environment specific utilities)Each of these files is configured to reference the appropriate dependencies and provide optimal type-checking for their specific context.
.nuxt/tsconfig.json. However, we recommend using TypeScript project references with the new configuration files (.nuxt/tsconfig.app.json, .nuxt/tsconfig.server.json, etc.) for better type safety and performance. This legacy file will be removed in a future version of Nuxt.Since the project is divided into multiple type contexts, it's important to augment types within the correct context to ensure they're properly recognized. TypeScript will not recognize augmentations placed outside these directories unless they are explicitly included in the appropriate context.
For example, if you want to augment types for the app context, the augmentation file should be placed in the app/ directory.
Similarly:
server context, place the augmentation file in the server/ directory.shared/ directory.TypeScript comes with certain checks to give you more safety and analysis of your program.
Strict checks are enabled by default in Nuxt when the typescript.typeCheck option is enabled to give you greater type safety.
If you are currently converting your codebase to TypeScript, you may want to temporarily disable strict checks by setting strict to false in your nuxt.config:
export default defineNuxtConfig({
typescript: {
strict: false,
},
})