tsconfig.json 的配置很多,大致内容如下
tsconfig.json
_109{_109 "compilerOptions": {_109 /* Visit https://aka.ms/tsconfig to read more about this file */_109_109 /* Projects */_109 // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */_109 // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */_109 // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */_109 // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */_109 // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */_109 // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */_109_109 /* Language and Environment */_109 "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,_109 // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */_109 // "jsx": "preserve", /* Specify what JSX code is generated. */_109 // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */_109 // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */_109 // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */_109 // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */_109 // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */_109 // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */_109 // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */_109 // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */_109 // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */_109_109 /* Modules */_109 "module": "commonjs" /* Specify what module code is generated. */,_109 // "rootDir": "./", /* Specify the root folder within your source files. */_109 // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */_109 // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */_109 // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */_109 // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */_109 // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */_109 // "types": [], /* Specify type package names to be included without being referenced in a source file. */_109 // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */_109 // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */_109 // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */_109 // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */_109 // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */_109 // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */_109 // "resolveJsonModule": true, /* Enable importing .json files. */_109 // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */_109 // "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */_109_109 /* JavaScript Support */_109 // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */_109 // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */_109 // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */_109_109 /* Emit */_109 // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */_109 // "declarationMap": true, /* Create sourcemaps for d.ts files. */_109 // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */_109 // "sourceMap": true, /* Create source map files for emitted JavaScript files. */_109 // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */_109 // "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. */_109 // "outDir": "./", /* Specify an output folder for all emitted files. */_109 // "removeComments": true, /* Disable emitting comments. */_109 // "noEmit": true, /* Disable emitting files from a compilation. */_109 // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */_109 // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */_109 // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */_109 // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */_109 // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */_109 // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */_109 // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */_109 // "newLine": "crlf", /* Set the newline character for emitting files. */_109 // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */_109 // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */_109 // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */_109 // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */_109 // "declarationDir": "./", /* Specify the output directory for generated declaration files. */_109 // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */_109_109 /* Interop Constraints */_109 // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */_109 // "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. */_109 // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */_109 "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,_109 // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */_109 "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,_109_109 /* Type Checking */_109 "strict": true /* Enable all strict type-checking options. */,_109 // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */_109 // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */_109 // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */_109 // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */_109 // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */_109 // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */_109 // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */_109 // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */_109 // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */_109 // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */_109 // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */_109 // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */_109 // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */_109 // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */_109 // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */_109 // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */_109 // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */_109 // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */_109_109 /* Completeness */_109 // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */_109 "skipLibCheck": true /* Skip type checking all .d.ts files. */_109 }_109}
大致说一下一些难以理解的配置项
esModuleInterop
example.ts
_10import fsDefault from "fs"_10import * as fsNamespace from "fs"
通常来说 fsDefault 和 fsNamespace 是不同的,如果你打开了 esModuleInterop, 那么 fsDefault 和 fsNamespace 就是相同的,如果你碰到 import fs from 'fs' 报错的情况
那么应该是改配置没有打开的原因
noEmit
该配置会导致 tsc 不输出任何文件,一般当你需要用 tsc 只做类型检查,不生成代码的时候,你可以考虑打开该配置
include , files rootDir and outDir
如果使用 files 指定所有 ts 文件是很麻烦的
直接使用 "include":["src"] 可以涵盖该目录的所有文件
rootDir 指定输出代码所在的目录树的顶部 The compilerOptions.rootDir option defines the root of the tree at outDir , 默认如果文件在 rootDir 不在 include 会有一些错误提示
moduleResolution
查找文件的方式,建议就是 node, 规定了 import 查找文件的方式
paths
paths 和 baseUrl 指定了在 import 的时候时候如何查找文件,但是它不负责帮你转换成相对路径,所以如果你在使用 tsc ,并将它用于库的时候,请使用 tsc-alias 或者禁用 baseUrl 属性
tsconfig.json
_10{_10 "baseUrl": ".", // baseUrl不可少_10 "paths": {_10 // 映射列表_10 "@/*": ["src/*"],_10 "moduleA": ["src/libs/moduleA"]_10 }_10}