feat: typescript options (#1940)

This commit is contained in:
pooya parsa 2021-11-16 13:32:21 +01:00 committed by GitHub
parent 5f5eb39ae4
commit b16cfea689
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 4 deletions

View File

@ -29,3 +29,19 @@ This file contains the recommended basic TypeScript configuration for your proje
::alert{icon=👉} ::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. 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 youve 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
}
})
```

View File

@ -9,6 +9,7 @@ import router from './router'
import server from './server' import server from './server'
import cli from './cli' import cli from './cli'
import generate from './generate' import generate from './generate'
import typescript from './typescript'
/* /*
TODO for top level normalizations: (nuxt2) TODO for top level normalizations: (nuxt2)
@ -38,5 +39,6 @@ export default {
router, router,
server, server,
cli, cli,
generate generate,
typescript
} }

View File

@ -0,0 +1,14 @@
export default {
/**
* TypeScript comes with certain checks to give you more safety and analysis of your program.
* Once youve 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<ReturnType<typeof import('pkg-types')['readPackageJSON']>>}
*/
tsConfig: {}
}

View File

@ -1,19 +1,20 @@
import { promises as fsp } from 'fs' import { promises as fsp } from 'fs'
import { join, relative, resolve } from 'pathe' import { join, relative, resolve } from 'pathe'
import { Nuxt, TSReference } from '@nuxt/kit' import { Nuxt, TSReference } from '@nuxt/kit'
import defu from 'defu'
import type { TSConfig } from 'pkg-types' import type { TSConfig } from 'pkg-types'
import { getModulePaths, getNearestPackage } from './cjs' import { getModulePaths, getNearestPackage } from './cjs'
export const writeTypes = async (nuxt: Nuxt) => { export const writeTypes = async (nuxt: Nuxt) => {
const modulePaths = getModulePaths(nuxt.options.modulesDir) const modulePaths = getModulePaths(nuxt.options.modulesDir)
const tsConfig: TSConfig = { const tsConfig: TSConfig = defu(nuxt.options.typescript?.tsConfig, {
compilerOptions: { compilerOptions: {
target: 'ESNext', target: 'ESNext',
module: 'ESNext', module: 'ESNext',
moduleResolution: 'Node', moduleResolution: 'Node',
skipLibCheck: true, skipLibCheck: true,
strict: true, strict: nuxt.options.typescript.strict,
allowJs: true, allowJs: true,
noEmit: true, noEmit: true,
resolveJsonModule: true, resolveJsonModule: true,
@ -27,7 +28,7 @@ export const writeTypes = async (nuxt: Nuxt) => {
join(relative(nuxt.options.buildDir, nuxt.options.rootDir), '**/*'), join(relative(nuxt.options.buildDir, nuxt.options.rootDir), '**/*'),
...nuxt.options.srcDir !== nuxt.options.rootDir ? [join(relative(nuxt.options.buildDir, nuxt.options.srcDir), '**/*')] : [] ...nuxt.options.srcDir !== nuxt.options.rootDir ? [join(relative(nuxt.options.buildDir, nuxt.options.srcDir), '**/*')] : []
] ]
} })
const aliases = { const aliases = {
...nuxt.options.alias, ...nuxt.options.alias,