tsconfig.json
是TypeScript編譯器的配置文件桃移,用于指定編譯TypeScript代碼時的編譯選項和編譯目標(biāo)等信息炮赦。通過修改該文件吟秩,可以定制TypeScript編譯器的行為俗扇,例如指定編譯目標(biāo)慌闭、啟用或禁用特定的語言特性、設(shè)置代碼檢查規(guī)則等查排。
以下是一些常用的tsconfig.json
選項(12個):
-
compilerOptions
:編譯器選項凳枝,用于配置TypeScript編譯器的行為,包括指定目標(biāo)JS版本雹嗦、模塊化方式范舀、輸出目錄、是否生成source map等了罪。 -
files
:指定需要編譯的文件列表锭环,可以是相對或絕對路徑。 -
include
:包含需要編譯的文件的glob路徑模式泊藕。 -
exclude
:排除不需要編譯的文件的glob路徑模式辅辩。 -
extends
:用于擴(kuò)展其他tsconfig.json配置,可以被配置文件繼承娃圆。 -
compileOnSave
:是否在保存文件時自動編譯玫锋。 -
references
:使用項目引用,可以將一個項目作為另一個項目的依賴讼呢。 -
typeAcquisition
:自動獲取類型聲明的設(shè)置撩鹿。 -
angularCompilerOptions
:幫助編譯Angular應(yīng)用程序的特殊編譯器選項。 -
excludeDirectories
:確定不應(yīng)該被搜索的頂級目錄悦屏。 -
excludeFiles
:指定應(yīng)該被忽略的文件节沦。 -
includeDirectories
:包含需要編譯的文件的目錄列表。
通過在項目根目錄下創(chuàng)建tsconfig.json
文件础爬,并配置以上選項甫贯,可以定制TypeScript編譯器的行為,以滿足項目的需求看蚜。
compilerOptions
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */
/* Projects */
// "incremental": true, /* 保存.tsbuildinfo文件以允許增量編譯項目叫搁。*/
// "composite": true, /* 啟用允許TypeScript項目與項目引用一起使用的約束。*/
// "tsBuildInfoFile": "./.tsbuildinfo", /* 指定.tsbuildinfo增量編譯文件的路徑供炎。 */
// "disableSourceOfProjectReferenceRedirect": true, /* 在引用復(fù)合項目時渴逻,禁用首選源文件而不是聲明文件。 */
// "disableSolutionSearching": true, /* 編輯時將項目從多項目參照檢查中選擇出來碱茁。 */
// "disableReferencedProjectLoad": true, /* 減少TypeScript自動加載的項目數(shù)量裸卫。 */
/* Language and Environment */
"target": "es2016", /* 指定編譯后的 JavaScript 代碼的目標(biāo)版本∨ⅲ可選值包括 "es3"墓贿、"es5"、"es6"蜓氨、"es2015"聋袋、"es2016"、"es2017"穴吹、"es2018"幽勒、"es2019"、"es2020"港令、"es2021"啥容、"esnext"锈颗。默認(rèn)值為 "es3"。 */
// "lib": [], /* 指定一組描述目標(biāo)運(yùn)行時環(huán)境的綁定庫聲明文件咪惠。*/
// "jsx": "preserve", /* 指定 JSX 的處理方式击吱。可選值包括 "preserve"遥昧、"react"覆醇、"react-jsx"、"react-jsxdev"炭臭、"react-native"永脓。默認(rèn)值為 "preserve"。 */
// "experimentalDecorators": true, /*啟用對遺留實(shí)驗(yàn)性裝飾器的實(shí)驗(yàn)性支持鞋仍。 */
// "emitDecoratorMetadata": true, /* 為源文件中的修飾聲明發(fā)出設(shè)計類型元數(shù)據(jù)常摧。 */
// "jsxFactory": "", /* 指定針對React JSX emit時使用的JSX工廠函數(shù),例如“React.createElement”或“h”威创。 */
// "jsxFragmentFactory": "", /* 指定針對React JSX發(fā)射的片段時用于片段的JSX片段引用排宰,例如“React.Fraggment”或“Fragment”。 */
// "jsxImportSource": "", /*指定在使用“JSX:areact JSX*”時用于導(dǎo)入JSX工廠函數(shù)的模塊說明符那婉。 */
// "reactNamespace": "", /* 指定為“createElement”調(diào)用的對象板甘。這僅適用于以“反應(yīng)”JSX發(fā)射為目標(biāo)的情況。 */
// "noLib": true, /* 禁用包含任何庫文件详炬,包括默認(rèn)的lib.d.ts盐类。 */
// "useDefineForClassFields": true, /* 發(fā)出符合ECMAScript標(biāo)準(zhǔn)的類字段。*/
// "moduleDetection": "auto", /* 控制使用何種方法檢測模塊格式的JS文件呛谜。 */
/* Modules */
"module": "commonjs", /* 指定生成的模塊代碼在跳。*/
// "rootDir": "./", /* 指定源文件中的根文件夾。 */
// "moduleResolution": "node10", /* 指定TypeScript如何從給定的模塊說明符中查找文件隐岛。 */
// "baseUrl": "./", /* 用于解析非相對模塊名稱的基本目錄猫妙。默認(rèn)值為 undefined。 */
// "paths": {}, /* 指定模塊名到基于 baseUrl 的路徑映射聚凹。默認(rèn)值為 undefined割坠。 */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
/* JavaScript Support */
// "allowJs": true, /* 允許JavaScript文件成為程序的一部分。使用“checkJS”選項可以從這些文件中獲取錯誤妒牙。 */
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
/* Emit */
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
// "outDir": "./", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
// "newLine": "crlf", /* Set the newline character for emitting files. */
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
/* Interop Constraints */
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
// "allowUnusedLabels": true, /* 禁用未使用標(biāo)簽的錯誤報告彼哼。 */
// "allowUnreachableCode": true, /* 禁用無法訪問的代碼的錯誤報告 */
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}
常用配置
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"strict": true,
"noLib": false,
"forceConsistentCasingInFileNames": true,
"allowSyntheticDefaultImports": true,
"strictFunctionTypes": false,
"jsx": "preserve",
"baseUrl": ".",
"allowJs": true,
"sourceMap": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"experimentalDecorators": true,
"lib": ["dom", "esnext"],
"noImplicitAny": false,
"skipLibCheck": true,
"types": ["vite/client"],
"removeComments": true,
"paths": {
"/@/*": ["src/*"],
"/#/*": ["types/*"]
}
},
"include": [
"tests/**/*.ts",
"src/**/*.ts",
"src/**/*.d.ts",
"src/**/*.tsx",
"src/**/*.vue",
"types/**/*.d.ts",
"types/**/*.ts",
"build/**/*.ts",
"build/**/*.d.ts",
"mock/**/*.ts",
"vite.config.ts"
],
"exclude": ["node_modules", "tests/server/**/*.ts", "dist", "**/*.js"]
}