tsconfig.json應(yīng)用

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"]
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市湘今,隨后出現(xiàn)的幾起案子敢朱,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拴签,死亡現(xiàn)場離奇詭異孝常,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)蚓哩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進(jìn)店門茫因,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人杖剪,你說我怎么就攤上這事〕鄞” “怎么了盛嘿?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長括袒。 經(jīng)常有香客問我次兆,道長,這世上最難降的妖魔是什么锹锰? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任芥炭,我火速辦了婚禮,結(jié)果婚禮上恃慧,老公的妹妹穿的比我還像新娘园蝠。我一直安慰自己,他們只是感情好痢士,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布彪薛。 她就那樣靜靜地躺著,像睡著了一般怠蹂。 火紅的嫁衣襯著肌膚如雪善延。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天城侧,我揣著相機(jī)與錄音易遣,去河邊找鬼。 笑死嫌佑,一個胖子當(dāng)著我的面吹牛豆茫,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播屋摇,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼澜薄,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了摊册?” 一聲冷哼從身側(cè)響起肤京,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后忘分,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體棋枕,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年妒峦,在試婚紗的時候發(fā)現(xiàn)自己被綠了重斑。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡肯骇,死狀恐怖窥浪,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情笛丙,我是刑警寧澤漾脂,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站胚鸯,受9級特大地震影響骨稿,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜姜钳,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一坦冠、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧哥桥,春花似錦辙浑、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至已卸,卻和暖如春佛玄,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背累澡。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工梦抢, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人愧哟。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓奥吩,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蕊梧。 傳聞我的和親對象是個殘疾皇子霞赫,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容