• 欢迎大家分享资料!前往留言板评论即可!

TypeScript之配置文件(tsconfig.json)

合宙 模组资料网 1年前 (2021-05-15) 341次浏览 已收录 0个评论 扫描二维码
文章目录[隐藏]

再续前缘

之前已经对ts进行了一个基础的认识,ta的基本类型以及声明方式,同时也对接口这个概念进行了一个简单的说明。那和用其他语言进行项目开发的时候,也是存在进行文件编译配置的。

初识tsconfig.json文件

以自己当前实际项目为例,你可以通过命令行:

tsc --init   //初始化一个编译配置文件

运行完成之后,就能看到一个tsconfig.json文件,这个用于编译配置。简单看一下(下面看一下我现在的编译配置):

{
  // "files": ["demo.ts"],
  // "exclude": ["demo.ts"],
  // "include": ["demo.ts"],
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */

    /* Basic Options */
    // "incremental": true,                   /* Enable incremental compilation */
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    // "lib": [],                             /* Specify library files to be included in the compilation. */
    // "allowJs": true,                       /* Allow javascript files to be compiled. */
    // "checkJs": true,                       /* Report errors in .js files. */
    // "jsx": "preserve",                     /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
    // "declaration": true,                   /* Generates corresponding '.d.ts' file. */
    // "declarationMap": true,                /* Generates a sourcemap for each corresponding '.d.ts' file. */
    // "sourceMap": true,                     /* Generates corresponding '.map' file. */
    "rootDir":"./src",   //ts文件
    "outDir":"./build", //编译后的文件
    // "outFile": "./",                       /* Concatenate and emit output to single file. */
    // "outDir": "./",                        /* Redirect output structure to the directory. */
    // "rootDir": "./",                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    // "composite": true,                     /* Enable project compilation */
    // "tsBuildInfoFile": "./",               /* Specify file to store incremental compilation information */
    "removeComments": true,                /* Do not emit comments to output. */
    // "noEmit": true,                        /* Do not emit outputs. */
    // "importHelpers": true,                 /* Import emit helpers from 'tslib'. */
    // "downlevelIteration": true,            /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
    // "isolatedModules": true,               /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

    /* Strict Type-Checking Options */
    // "strict": true,                           /* Enable all strict type-checking options. */
    // "noImplicitAny": true,                 /* Raise error on expressions and declarations with an implied 'any' type. */
    // "strictNullChecks": true,              /* Enable strict null checks. */
    // "strictFunctionTypes": true,           /* Enable strict checking of function types. */
    // "strictBindCallApply": true,           /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
    // "strictPropertyInitialization": true,  /* Enable strict checking of property initialization in classes. */
    // "noImplicitThis": true,                /* Raise error on 'this' expressions with an implied 'any' type. */
    // "alwaysStrict": true,                  /* Parse in strict mode and emit "use strict" for each source file. */

    /* Additional Checks */
    // "noUnusedLocals": true,                /* Report errors on unused locals. */
    // "noUnusedParameters": true,            /* Report errors on unused parameters. */
    // "noImplicitReturns": true,             /* Report error when not all code paths in function return a value. */
    // "noFallthroughCasesInSwitch": true,    /* Report errors for fallthrough cases in switch statement. */
    // "noUncheckedIndexedAccess": true,      /* Include 'undefined' in index signature results */

    /* Module Resolution Options */
    // "moduleResolution": "node",            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    // "baseUrl": "./",                       /* Base directory to resolve non-absolute module names. */
    // "paths": {},                           /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
    // "rootDirs": [],                        /* List of root folders whose combined content represents the structure of the project at runtime. */
    // "typeRoots": [],                       /* List of folders to include type definitions from. */
    // "types": [],                           /* Type declaration files to be included in compilation. */
    // "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
    "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    // "preserveSymlinks": true,              /* Do not resolve the real path of symlinks. */
    // "allowUmdGlobalAccess": true,          /* Allow accessing UMD globals from modules. */

    /* Source Map Options */
    // "sourceRoot": "",                      /* Specify the location where debugger should locate TypeScript files instead of source locations. */
    // "mapRoot": "",                         /* Specify the location where debugger should locate map files instead of generated locations. */
    // "inlineSourceMap": true,               /* Emit a single file with source maps instead of having a separate file. */
    // "inlineSources": true,                 /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

    /* Experimental Options */
    // "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
    // "emitDecoratorMetadata": true,         /* Enables experimental support for emitting type metadata for decorators. */

    /* Advanced Options */
    "skipLibCheck": true,                     /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true  /* Disallow inconsistently-cased references to the same file. */
  }
}

以上是初始化的ts配置文件,可以看到有很多的相关配置,说一下初级阶段常用的有哪些呢?

.json最外层属性

include

项目目录下哪些文件是需要进行编译的,路径表示:
["./项目自定义文件夹/**/*"]
* **:表示目标文件夹
* *:表示目标文件夹下的所有文件

ps:这里只是对ts文件进行编译o

exclude

项目目录下哪些文件是需要进行编译的,路径表示:
["./项目自定义文件夹/**/*"] 和include相似,功能相反;
“exclude”默认情况下会排除node_modules,bower_components,jspm_packages和目录。等还有很多,不需要进行编译的文件

compilerOptions

该属性为编译器选项,重要的是ta下的子属性是你编译时所需要进行配置的;可以看到初始化文件下是存在很多的,简单说明一下:
* target: “es2015″ //ts编译成的js版本 如果你灭有进行配置* 默认值为:”es3”
* module: “” //模块化规范设置 默认es2015,一般也不修改
* outDir: “./dist” //输出目录 设置编译后的文件所在的目录
* rootDir:”” //目标文件目录
* outFile:”./dist/xxx” //设置将所有编译后文件合并
* lib: “” //项目所需使用的库 通常不设置,使用默认值

  • “allowJs”:true //是否允许对写入js文件进行编译
  • “checkJs”:true //检查js文件是否符合模块化规范

  • “removeComments” :true //编译后的文件是否移除注释

  • “noEmit”:true //不生成编译后的文件,默认为false
  • “noEmitOnError”:true //报错后不生成编译文件

  • “strict”: true //是否启用严格 建议设置为true

  • “alwaysStrict”:false //编译文件是否使用严格模式
  • “noImplicitAny”:true //不允许隐式类型any
  • “noImplicitThis”:false //不允许不明确类型的this
  • “strictNullChecks”:false //严格是否为空

详细请去官网编译选项


转载请注明原文链接:TypeScript之配置文件(tsconfig.json)
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址