TypeScript 101 安装-编译

44 阅读1分钟

TypeScript 相比较JS添加了

  • 类型
  • 支持ES的新特性
  • 添加ES不具备的特性
  • 丰富的配置选项

开发环境搭建,安装NodeJS,JS的运行环境 juejin.cn/post/738769…

运行命令安装TS

  • npm install -g typescript
    

创建TS文件, 在Ternimal

  • 运行 tsc .\01_hello.ts    将Ts文件编辑为 js文件
    
  • 运行 tsc 编译文件下所有的ts文件
    

tsConfig.json // 配置需要被编译的Ts文件地址,包含 include,exclude 常用已加注释

    {
        "include":[
            "./src/**/*" //表示只编译src文件目录下的ts文件夹地址
        ],
        "exclude":[
            "exclude.ts" //不需要被编译的文件夹地址
         ],
         "files":[], // 指定需要编译的文件
         "compileOptions"{
            "baseUrl": "./", 
           
            "forceConsistentCasingInFileNames": true, 
            "strict": true,  // 严格检查总开关
            "noImplicitOverride": true, 
            "noPropertyAccessFromIndexSignature": true,
            "noImplicitReturns": true,
            "noFallthroughCasesInSwitch": true,
            "sourceMap": true,
            "declaration": false,
            "downlevelIteration": true,
            "experimentalDecorators": true,
            "moduleResolution": "node",
            "importHelpers": true,
            "useDefineForClassFields": false,
            
            "alwaysStrict": false //编译后的文件是否开启严格模式
            "noImplicitAny": //不允许隐形any
            //常用属性
            "target": "ES2022", // 指定需要被编译为的ES版本
            "module": "ES2022", // 指定要使用的模块化的规范
            "lib": [  // 指定项目中使用的库
              "ES2022",
              "dom"
            ],
            "outDir": "./dist/out-tsc", //编译后的文件目录 
            "outFile": "app.js" //将全局作用域中的代码合并为一个文件
            "allowJs": false 是否对JS文件进行编译,默认false
            "checkJs": false 检查js代码是否符合规范,默认false
            "removeComments" // 是否移除注释
            "noEmit" //不生成编译后的文件
            "noEmitOnError" //有错误时不生成编译后的文件
          }
    }