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=👉}
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 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
}

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 { 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,