- 安装typescript
npm i typescript -D
- 初始化typescript文件
npx tsc --init
- 目录下会生成一个tsconfig.json文件
/*
"include" 用来指定哪些 ts 文件需要被编译
路径:** 表示任意目录,*表示任意文件
默认值:["src"]
"exclude" 不需要被编译的文件目录
默认值:["node_modules", "bower_components", jspm_packages]
"files" 指定被编译的文件,只有文件少时才会用到
"complierOptions" 编译器选项
*/
{
"include": [
"./src/**/*"
],
"exclude": [
"./src/assets/**/*"
],
"compilerOptions": {
// 用来设置编译后的文件是否使用严格模式
"alwaysStrict": true,
// allowJs 是否对js文件进行编译,默认false
"allowJs": true,
// 允许通过 import x from ‘y’ 即使模块没有显式指定 default 导出
"allowSyntheticDefalutImports": true,
// 不报告执行不到的代码错误。
"allowUnreachableCode" : true,
// 不报告未使用的标签错误
"allowUnusedLabels": false,
// 允许在模块中全局变量的方式访问umd模块
"allowUmdGlobalAccess": true,
// 工作根目录
"baseUrl": ".",
// checkJs 是否检查js代码是否符合语法规范,默认false
"checkJs": true,
// 是否自动创建类型声明文件
"declaration": true,
// 类型声明文件的输出目录
"declarationDir": "./lib",
// 为声明文件生成sourceMap
"declarationMap": true,
// es 模块 互操作,屏蔽 ESModule 和 CommonJS 之间的差异
"esModuleInterop": true,
// 只生成声明文件,而不会生成js文件
"emitDeclarationOnly": true,
// 对文件名称强制区分大小写
"forceConsistentCasingInFileNames": true,
// 是否将没有 import/export 的文件视为(全局而非模块化)脚本文件.
"isolatedModules": true,
// 生成目标文件的inline SourceMap,inline SourceMap会包含在生成的js文件中
"inlineSourceMap": true,
// 指定将 JSX 编译成什么形式
"jsx": "react-jsx",
// lib 指定项目中要使用的库
"lib": [
"dom",
"dom.iterable",
"ESNext"
],
// 打印输出文件
"listEmittedFiles": true,
// 打印编译的文件(包括引用的声明文件)
"listFiles": true
// module 指定要使用的模块化的规范
// none, commonjs, amd, system, umd, es6, es2015, es2020, esnext
"module": "esnext",
// 模块解析策略
"moduleResolution": "node",
// 为 swith 语句启用错误报告
"noFallthroughCasesInSwitch": true,
// noEmitOnError 当有错误时不生成编译后产生的文件
"noEmitOnError": true,
// noEmit 不生成编译后产生的文件(只进行类型检查)
"noEmit": true,
// 不允许隐式的 any 类型
"noImplicitAny": true,
// 不允许不明确类型的 this
"noImplicitThis": true,
// 检查只声明、未使用的局部变量(只提示不报错)
"noUnusedLocals": true,
// 检查未使用的函数参数(只提示不报错)
"noUnusedParameters": true,
// 每个分支都会有返回值
"noImplicitReturns": true,
// 允许导入扩展名为 .json的模块
"resolveJsonModule": true,
// removeComments 是否移除注释
"removeComments": true,
// 跳过声明文件的类型检查
"skipLibCheck": true,
// 所有严格模式的总开关
"strict": true,
// 严格的检查空值
"strictNullChecks": true,
// 是否生成map文件
"sourceMap": true,
// 不允许函数参数双向协变
"strictFunctionTypes": true,
// 类的实例属性必须初始化
"strictPropertyInitialization": true,
// 严格的bind/call/apply检查
"strictBindCallApply": true,
// 指定引入的类型声明文件,默认是自动引入所有声明文件,一旦指定该选项,则会禁用自动引入,改为只引入指定的类型声明文件,如果指定空数组[]则不引用任何文件
"types": [
"node", // 引入 node 的类型声明
],
// 声明文件目录,默认时node_modules/@types
"typeRoots": [],
// target 用来指定ts被编译为的es版本
// es3, es5, es6, es2015, es2016, es2017, es2018, es2019, es2020,
"target": "es6",
// outDir 用来指定编译后文件所在的目录
"outDir": "./dist",
// outFile 将代码合并为一个文件
"outFile": "./dist.app.js",
// 指定模块的路径,和baseUrl有关联,和webpack中resolve.alias配置一样
"paths": {
"src": [ // 指定后可以在文件之直接 import * from 'src';
"./src"
],
},
}
}
本文介绍了如何在Node.js项目中安装TypeScript并详细解释了tsconfig.json文件的作用,包括编译规则、文件包含和排除、以及各种编译选项的设置。
86

被折叠的 条评论
为什么被折叠?



