对于typescript的编译,我们需要用到一些基本的配置文件,这些配置文件写在tsconfig.json中的。我们项目中的一个文件夹中添加一个tsconfig.json,则表明,在编译这个文件夹中的文件的时候,需要遵循我们在文件夹tsconfig.json中配置的编译选项。
但是在什么时候需要用到tsconfig.json呢,官网上给出的几个场景,
1、在调用tsc的时候,但是这个没有文件输入,这个时候,tsc会去搜寻文件夹中的tsconfig.json,进行编译,并且与父级文件夹进行连接。
2、当我们输入tsc --project 或则tsc -p的时候,会去寻找,项目中的tsconfig.json或者是制定的一个json选项文件。
当但我们在编译的时候,输入的文件名称,这个时候,就会忽略掉项目中的tsconfig.json文件。
举个例子,我们在项目中添加一个如下的tsconfig.json文件,
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true
},
"files": [
"core.ts"
]
}
然后呢我们在文件夹中添加如下内容。
项目目录
|
|--- core.ts
|
|--- tsconfig.json
然后呢 我们通过命令进入该项目目录下面,输入tsc 这个时候我们就能够看到,编辑的结果。
我们没有输入要编译的文件,但是能够把文件进行编译了,是因为我们在tsconfig.json的files中配置了我们要编译的文件,就是core.ts。所在我们输入tsc的时候,就会去寻找tsconfig.json然后根据这个文件中的内容进行编译。当然,除了files我们还能够使用include这个选项来进行配置我们所要进行编译的项目文件。
如下配置:
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true
},
"include": [
"core.ts"
]
}
这个的编译结果,跟上面的编译结果是一样的。但是inclue和files 还是有区别的。files一遍用于,相对位置的一些文件列表,而,include则最好配置一些目录什么的,比如说使用 **/*.ts之类的。
现在我们知道,我们能可以通过配置文件配置啊我们所要进行编译的文件,当然有些时候我们项目中有些文件是不希望被进行编译的,这个时候我们可以通过使用,exclude这个属性来进行操作。
compilerOptions:这个属性就是我们所要配置的编译属性。这配置项里面能够配置的项目比较多,
下面是官方给你全部的文档
Option | Type | Default | Description |
---|---|---|---|
--allowJs | boolean | true | 允许javascript进行编译. |
--allowSyntheticDefaultImports | boolean | module === "system" | Allow default imports from modules with no default export. This does not affect code emit, just typechecking. |
--allowUnreachableCode | boolean | false | Do not report errors on unreachable code. |
--allowUnusedLabels | boolean | false | Do not report errors on unused labels. |
--baseUrl | string | Base directory to resolve non-relative module names. See Module Resolution documentation for more details. | |
--charset | string | "utf8" | The character set of the input files. |
--declaration -d | boolean | false | Generates corresponding ‘.d.ts’ file. |
--declarationDir | string | Output directory for generated declaration files. | |
--diagnostics | boolean | false | Show diagnostic information. |
--disableSizeLimit | boolean | false | Disable size limitation on JavaScript project. |
--emitBOM | boolean | false | Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. |
--emitDecoratorMetadata [1] | boolean | false | Emit design-type metadata for decorated declarations in source. See issue #2577 for details. |
--experimentalDecorators [1] | boolean | false | Enables experimental support for ES7 decorators. |
--forceConsistentCasingInFileNames | boolean | false | Disallow inconsistently-cased references to the same file. |
--help -h | Print help message. | ||
--inlineSourceMap | boolean | false | Emit a single file with source maps instead of having a separate file. |
--inlineSources | boolean | false | Emit the source alongside the sourcemaps within a single file; requires --inlineSourceMap or --sourceMap to be set. |
--init | Initializes a TypeScript project and creates a tsconfig.json file. | ||
--isolatedModules | boolean | false | Unconditionally emit imports for unresolved files. |
--jsx | string | "Preserve" | Support JSX in ‘.tsx’ files: 'React' or'Preserve' . See JSX. |
--lib | string[] | List of library files to be included in the compilation.</br>Possible values are: ► es5 ► es6 ► es2015 ► es7 ► es2016 ► es2017 ► dom ► webworker ► scripthost ► es2015.core ► es2015.collection ► es2015.generator ► es2015.iterable ► es2015.promise ► es2015.proxy ► es2015.reflect ► es2015.symbol ► es2015.symbol.wellknown ► es2016.array.include ► es2017.object ► es2017.sharedmemory Note: If --lib is not specified a default library is injected. The default library injected is: ► For --target ES5 :dom,es5,scripthost </br> ► For --target ES6 :dom,es6,dom.iterable,scripthost | |
--listEmittedFiles | boolean | false | Print names of generated files part of the compilation. |
--listFiles | boolean | false | Print names of files part of the compilation. |
--locale | string | (platform specific) | The locale to use to show error messages, e.g. en-us. |
--mapRoot | string | Specifies the location where debugger should locate map files instead of generated locations. Use this flag if the .map files will be located at run-time in a different location than the .js files. The location specified will be embedded in the sourceMap to direct the debugger where the map files will be located. | |
--maxNodeModuleJsDepth | number | 0 | The maximum dependency depth to search under node_modules and load JavaScript files. Only applicable with --allowJs . |
--module -m | string | target === 'ES6' ? 'ES6' : 'commonjs' | Specify module code generation: 'none' ,'commonjs' , 'amd' , 'system' ,'umd' , 'es6' , or 'es2015' .► Only 'amd' and 'system' can be used in conjunction with --outFile .► 'es6' and 'es2015' values may not be used when targeting ES5 or lower. |
--moduleResolution | string | module === 'amd' | 'system' | 'ES6' ? 'classic' : 'node' | Determine how modules get resolved. Either 'node' for Node.js/io.js style resolution, or 'classic' . See Module Resolution documentation for more details. |
--newLine | string | (platform specific) | Use the specified end of line sequence to be used when emitting files: 'crlf' (windows) or 'lf' (unix).” |
--noEmit | boolean | false | Do not emit outputs. |
--noEmitHelpers | boolean | false | Do not generate custom helper functions like __extends in compiled output. |
--noEmitOnError | boolean | false | Do not emit outputs if any errors were reported. |
--noFallthroughCasesInSwitch | boolean | false | Report errors for fallthrough cases in switch statement. |
--noImplicitAny | boolean | false | Raise error on expressions and declarations with an implied ‘any’ type. |
--noImplicitReturns | boolean | false | Report error when not all code paths in function return a value. |
--noImplicitThis | boolean | false | Raise error on this expressions with an implied ‘any’ type. |
--noImplicitUseStrict | boolean | false | 在模块二中,不会自动添加"use strict" |
--noLib | boolean | false | 不去加载默认include库 |
--noResolve | boolean | false | Do not add triple-slash references or module import targets to the list of compiled files. |
--noUnusedLocals | boolean | false | Report errors on unused locals. |
--noUnusedParameters | boolean | false | Report errors on unused parameters. |
~~--out ~~ | string | DEPRECATED. Use --outFile instead. | |
--outDir | string | 编译后的输出文件夹 | |
--outFile | string | Concatenate and emit output to single file. The order of concatenation is determined by the list of files passed to the compiler on the command line along with triple-slash references and imports. See output file order documentation for more details. | |
paths [2] | Object | List of path mapping entries for module names to locations relative to thebaseUrl . See Module Resolution documentation for more details. | |
--preserveConstEnums | boolean | false | Do not erase const enum declarations in generated code. See const enums documentation for more details. |
--pretty [1] | boolean | false | Stylize errors and messages using color and context. |
--project -p | string | Compile a project given a valid configuration file. The argument can be an file path to a valid JSON configuration file, or a directory path to a directory containing a tsconfig.json file.See tsconfig.json documentation for more details. | |
--reactNamespace | string | "React" | Specifies the object invoked forcreateElement and __spread when targeting ‘react’ JSX emit. |
--removeComments | boolean | false | Remove all comments except copy-right header comments beginning with /*! |
--rootDir | string | (common root directory is computed from the list of input files) | Specifies the root directory of input files. Only use to control the output directory structure with --outDir . |
rootDirs [2] | string[] | List of root folders whose combined content represent the structure of the project at runtime. See Module Resolution documentation for more details. | |
--skipLibCheck | boolean | false | Don’t check a the default library (lib.d.ts ) file’s validity. |
--skipDefaultLibCheck | boolean | false | Don’t check a user-defined default library (*.d.ts ) file’s validity. |
--sourceMap | boolean | false | Generates corresponding ‘.map’ file. |
--sourceRoot | string | Specifies the location where debugger should locate TypeScript files instead of source locations. Use this flag if the sources will be located at run-time in a different location than that at design-time. The location specified will be embedded in the sourceMap to direct the debugger where the source files will be located. | |
--strictNullChecks | boolean | false | In strict null checking mode, the null and undefined values are not in the domain of every type and are only assignable to themselves and any (the one exception being that undefined is also assignable to void ). |
--stripInternal [1] | boolean | false | Do not emit declarations for code that has an /** @internal */ JSDoc annotation. |
--suppressExcessPropertyErrors | boolean | false | Suppress excess property checks for object literals. |
--suppressImplicitAnyIndexErrors | boolean | false | Suppress --noImplicitAny errors for indexing objects lacking index signatures. See issue #1232 for more details. |
--target -t | string | "ES3" | Specify ECMAScript target version: 'es3' (default), 'es5' , or 'es6' . |
--traceResolution | boolean | false | Report module resolution log messages. |
--types | string[] | List of names of type definitions to include. | |
--typeRoots | string[] | List of folders to include type definitions from. | |
--version -v | Print the compiler’s version. | ||
--watch -w | Run the compiler in watch mode. Watch input files and trigger recompilation on changes. |