diff --git a/docs/content/2.concepts/3.typescript.md b/docs/content/2.concepts/3.typescript.md index 026692a005..baf6c49d1c 100644 --- a/docs/content/2.concepts/3.typescript.md +++ b/docs/content/2.concepts/3.typescript.md @@ -29,3 +29,19 @@ This file contains the recommended basic TypeScript configuration for your proje ::alert{icon=👉} Nitro also [auto-generates types](/concepts/server-engine#typed-api-routes) for API routes. Plus, Nuxt also generates types for globally available components and [auto-imports from your composables](/docs/directory-structure/composables), plus other core functionality. :: + +## Stricter Checks + +TypeScript comes with certain checks to give you more safety and analysis of your program. + +Once you’ve converted your codebase to TypeScript and felt familiar with, you can start enabling these checks for greater safety. ([read more](https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html#getting-stricter-checks)) + +In order to enable strict type checking, you have to update `nuxt.confg`: + +```js +export default defineNuxtConfig({ + typescript: { + strict: true + } +}) +``` diff --git a/packages/kit/src/config/schema/index.ts b/packages/kit/src/config/schema/index.ts index a44ff18a93..89ba6c0e6e 100644 --- a/packages/kit/src/config/schema/index.ts +++ b/packages/kit/src/config/schema/index.ts @@ -9,6 +9,7 @@ import router from './router' import server from './server' import cli from './cli' import generate from './generate' +import typescript from './typescript' /* TODO for top level normalizations: (nuxt2) @@ -38,5 +39,6 @@ export default { router, server, cli, - generate + generate, + typescript } diff --git a/packages/kit/src/config/schema/typescript.ts b/packages/kit/src/config/schema/typescript.ts new file mode 100644 index 0000000000..16e50d10fb --- /dev/null +++ b/packages/kit/src/config/schema/typescript.ts @@ -0,0 +1,14 @@ +export default { + /** + * TypeScript comes with certain checks to give you more safety and analysis of your program. + * Once you’ve converted your codebase to TypeScript, you can start enabling these checks for greater safety. + * [Read More](https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html#getting-stricter-checks) + */ + strict: false, + + /** + * You can extend generated `.nuxt/tsconfig.json` using this option + * @typedef {Awaited>} + */ + tsConfig: {} +} diff --git a/packages/nuxi/src/utils/prepare.ts b/packages/nuxi/src/utils/prepare.ts index f07ef1b748..c7e42de69c 100644 --- a/packages/nuxi/src/utils/prepare.ts +++ b/packages/nuxi/src/utils/prepare.ts @@ -1,19 +1,20 @@ import { promises as fsp } from 'fs' import { join, relative, resolve } from 'pathe' import { Nuxt, TSReference } from '@nuxt/kit' +import defu from 'defu' import type { TSConfig } from 'pkg-types' import { getModulePaths, getNearestPackage } from './cjs' export const writeTypes = async (nuxt: Nuxt) => { const modulePaths = getModulePaths(nuxt.options.modulesDir) - const tsConfig: TSConfig = { + const tsConfig: TSConfig = defu(nuxt.options.typescript?.tsConfig, { compilerOptions: { target: 'ESNext', module: 'ESNext', moduleResolution: 'Node', skipLibCheck: true, - strict: true, + strict: nuxt.options.typescript.strict, allowJs: true, noEmit: true, resolveJsonModule: true, @@ -27,7 +28,7 @@ export const writeTypes = async (nuxt: Nuxt) => { join(relative(nuxt.options.buildDir, nuxt.options.rootDir), '**/*'), ...nuxt.options.srcDir !== nuxt.options.rootDir ? [join(relative(nuxt.options.buildDir, nuxt.options.srcDir), '**/*')] : [] ] - } + }) const aliases = { ...nuxt.options.alias,