关于typescript:TypeScript安装和使用

3次阅读

共计 2927 个字符,预计需要花费 8 分钟才能阅读完成。

环境搭建

  • 须要下载安装 Node.js
  • npm 全局装置 typescript

    • npm i typescript -g

简略应用

  • 创立后缀为 .ts 的文件
  • 编译文件

    • 单个文件:

      • 编译:tsc xxx.ts
      • 继续监督:tsc xxx.ts -w 代表始终监督这个文件,批改这个文件中的代码后主动编译文件,然而只会监督这一个文件
    • 多个文件:须要先创立一个叫 tsconfig.json 的文件,哪怕是一个空的也能够

      • 编译:tsc
      • 继续监督:tsc -w 或者 tsc --watch
      • tsconfig.json 罕用配置:这个配置咱们既能够手动新建也能够通过命令实现,命令是 tsc --init

        /*
            tsconfig.json 是 ts 的编译器的配置文件,ts 编译器能够依据它的信息来对代码进行编译
                - "include": 用来指定哪些 ts 文件须要编译
                    - ** 示意任意目录
                    - * 示意任意文件
                - "exculde": 用来指定哪些不须要编译的
                    - 有默认值:["node_modules","bower_components","jspm_packages"]
                    - 额定排除的话和 include 一样的应用形式
                - "extends":定义被继承的配置文件
                - "files":指定被编译的文件的列表,只有须要编译的文件少时才会用到
                - "compilerOptions":编译器的配置
        */
        {
            "include": ["./src/**/*"],
            "compilerOptions": {
                // target:用来指定咱们 ts 编译成什么版本的 js
                "target": "ES3",
        
                // module:指定要应用的模块化的标准
                "module": "ES6",
        
                // lib:用来指定我的项目中要应用的库,一般来说不须要改
                // "lib": []
                
                // outDir:指定编译后文件缩放的目录
                "outDir": "./dist/js",
        
                // outFile:将代码合并成一个文件
                // "outFile": "./dist/js/app.js"
        
                // allowJs:是否对 js 文件进行编译,默认是 false
                "allowJs": false,
        
                // checkJs:是否查看 js 代码是否合乎语法标准,默认是 false
                "checkJs": false,
        
                // removeComments:是否移除正文, 默认 false
                "removeComments": false,
        
                // noEmit:不生成编译后的文件,编译其实执行了,然而就是不生成最初的文件,多用于查看下语法,其余用的不多,默认 false
                "noEmit": false,
        
                // noEmitOnError:当有错的时候不生成编译后的文件,默认 false
                "noEmitOnError": true,
        
                // 所有严格模式的中开关
                "strict": false,
        
                // alwaysStrict:编译后是否应用严格模式,默认为 false
                "alwaysStrict": false,
        
                // noImplicitAny:不容许隐式的应用 any, 默认 false
                "noImplicitAny": true,
        
                // noImplicitThis:不容许不明确的 this,默认 false
                "noImplicitThis": false,
        
                // strictNullChecks:严格查看空值,默认 false
                "strictNullChecks": false
            }
        }

webpack 中应用 ts

  • package.json 中配置

    {
    "name": "ts2",
    "version": "1.0.0",
    "description": "","main":"index.js","scripts": {"test":"echo \"Error: no test specified\" && exit 1","build":"webpack","dev":"webpack serve"},"author":"",
    "license": "ISC",
    "devDependencies": {
      "@babel/core": "^7.17.7",
      "@babel/preset-env": "^7.16.11",
      "babel-loader": "^8.2.3",
      "clean-webpack-plugin": "^4.0.0",
      "core-js": "^3.21.1",
      "html-webpack-plugin": "^5.5.0",
      "ts-loader": "^9.2.8",
      "typescript": "^4.6.2",
      "webpack": "^5.70.0",
      "webpack-cli": "^4.9.2",
      "webpack-dev-server": "^4.7.4"
    }
    }
    
  • webpack.config.js 中配置

    // 引入 node 的门路辨认模块
    const {resolve} = require('path')
    // 引入 html 主动生成插件
    const HtmlWebpackPlugin = require('html-webpack-plugin')
    // 引入每次打包前先革除上一次打包后果的插件
    const {CleanWebpackPlugin} = require('clean-webpack-plugin')
    
    
    module.exports = {
      // 配置入口文件
      entry: './src/index.ts',
      // 配置输入文件
      output: {path: resolve(__dirname,'dist'),
          filename: 'built.js',
          // 配置打包环境的
          environment: {
              // 通知 webpack 别应用箭头函数
              arrowFunction: false
          }
      },
      module: {
          // 配置 webpack 对模块的打包规定
          rules: [
              {
                  // 配置对 ts 文件的打包编译规定
                  test: /\.ts$/,
                  use: [
                      // 配置 babel
                      {
                          loader: 'babel-loader',
                          // 设置 babel
                          options: {
                              // 配置预约义的环境
                              presets: [
                                  [
                                      // 指定环境的插件
                                      "@babel/preset-env",
                                      // 配置信息
                                      {   
                                          // 要兼容的指标浏览器
                                          targets: {
                                              "chrome": "88",
                                              "ie": "11"
                                          },
                                          // 指定 corejs 的版本
                                          "corejs": "3",
                                          // 应用 corejs 的形式。"usage" 示意按需加载
                                          "useBuiltIns": "usage"
                                      }
                                  ]
                              ]
                          }
                      },
                      'ts-loader'
                  ],
                  exclude: /node_module/
              }
          ]
      },
      plugins: [
          // 革除上一次的打包的文件夹
          new CleanWebpackPlugin(),
          // 打包后的文件夹中主动生成一个 index.html 文件
          new HtmlWebpackPlugin({
              // 依照这个 index.html 文件去生成
              template: './src/index.html'
          })
      ],
      // 设置打包的环境
      mode: "development",
      // 批改文件自动更新
      devServer: {
          // 主动关上浏览器
          open: true
      },
      resolve: {extensions: ['.js','.ts','json']
      }
    }
  • tsconfig.js 中配置

    {
      "compilerOptions": {
          "module": "ES2015",
          "target": "ES2015",
          "strict": true
      }
    }
正文完
 0