typescript教程 (4)

对于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:这个属性就是我们所要配置的编译属性。这配置项里面能够配置的项目比较多,
下面是官方给你全部的文档

OptionTypeDefaultDescription
--allowJsbooleantrue允许javascript进行编译.
--allowSyntheticDefaultImportsbooleanmodule === "system"Allow default imports from modules with no default export. This does not affect code emit, just typechecking.
--allowUnreachableCodebooleanfalseDo not report errors on unreachable code.
--allowUnusedLabelsbooleanfalseDo not report errors on unused labels.
--baseUrlstring Base directory to resolve non-relative module names. See Module Resolution documentation for more details.
--charsetstring"utf8"The character set of the input files.
--declaration
-d
booleanfalseGenerates corresponding ‘.d.ts’ file.
--declarationDirstring Output directory for generated declaration files.
--diagnosticsbooleanfalseShow diagnostic information.
--disableSizeLimitbooleanfalseDisable size limitation on JavaScript project.
--emitBOMbooleanfalseEmit a UTF-8 Byte Order Mark (BOM) in the beginning of output files.
--emitDecoratorMetadata[1]booleanfalseEmit design-type metadata for decorated declarations in source. See issue #2577 for details.
--experimentalDecorators[1]booleanfalseEnables experimental support for ES7 decorators.
--forceConsistentCasingInFileNamesbooleanfalseDisallow inconsistently-cased references to the same file.
--help
-h
  Print help message.
--inlineSourceMapbooleanfalseEmit a single file with source maps instead of having a separate file.
--inlineSourcesbooleanfalseEmit 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.
--isolatedModulesbooleanfalseUnconditionally emit imports for unresolved files.
--jsxstring"Preserve"Support JSX in ‘.tsx’ files: 'React' or'Preserve'. See JSX.
--libstring[] 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
--listEmittedFilesbooleanfalsePrint names of generated files part of the compilation.
--listFilesbooleanfalsePrint names of files part of the compilation.
--localestring(platform specific)The locale to use to show error messages, e.g. en-us.
--mapRootstring 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.
--maxNodeModuleJsDepthnumber0The maximum dependency depth to search under node_modules and load JavaScript files. Only applicable with --allowJs.
--module
-m
stringtarget === '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.
--moduleResolutionstringmodule === '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.
--newLinestring(platform specific)Use the specified end of line sequence to be used when emitting files: 'crlf'(windows) or 'lf' (unix).”
--noEmitbooleanfalseDo not emit outputs.
--noEmitHelpersbooleanfalseDo not generate custom helper functions like __extends in compiled output.
--noEmitOnErrorbooleanfalseDo not emit outputs if any errors were reported.
--noFallthroughCasesInSwitchbooleanfalseReport errors for fallthrough cases in switch statement.
--noImplicitAnybooleanfalseRaise error on expressions and declarations with an implied ‘any’ type.
--noImplicitReturnsbooleanfalseReport error when not all code paths in function return a value.
--noImplicitThisbooleanfalseRaise error on this expressions with an implied ‘any’ type.
--noImplicitUseStrictbooleanfalse在模块二中,不会自动添加"use strict" 
--noLibbooleanfalse不去加载默认include库
--noResolvebooleanfalseDo not add triple-slash references or module import targets to the list of compiled files.
--noUnusedLocalsbooleanfalseReport errors on unused locals.
--noUnusedParametersbooleanfalseReport errors on unused parameters.
~~--out~~string DEPRECATED. Use --outFile instead.
--outDirstring 编译后的输出文件夹
--outFilestring 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.
--preserveConstEnumsbooleanfalseDo not erase const enum declarations in generated code. See const enums documentation for more details.
--pretty[1]booleanfalseStylize 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 atsconfig.json file.
See tsconfig.json documentation for more details.
--reactNamespacestring"React"Specifies the object invoked forcreateElement and __spread when targeting ‘react’ JSX emit.
--removeCommentsbooleanfalseRemove all comments except copy-right header comments beginning with /*!
--rootDirstring(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.
--skipLibCheckbooleanfalseDon’t check a the default library (lib.d.ts) file’s validity.
--skipDefaultLibCheckbooleanfalseDon’t check a user-defined default library (*.d.ts) file’s validity.
--sourceMapbooleanfalseGenerates corresponding ‘.map’ file.
--sourceRootstring 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.
--strictNullChecksbooleanfalseIn strict null checking mode, the nulland 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]booleanfalseDo not emit declarations for code that has an /** @internal */ JSDoc annotation.
--suppressExcessPropertyErrorsbooleanfalseSuppress excess property checks for object literals.
--suppressImplicitAnyIndexErrorsbooleanfalseSuppress --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'.
--traceResolutionbooleanfalseReport module resolution log messages.
--typesstring[] List of names of type definitions to include.
--typeRootsstring[] 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.

 

转载于:https://my.oschina.net/shuinian/blog/757398

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值