在react中使用TS

93 阅读1分钟
  1. 安装npm包
npm install --save typescript @types/node @types/react @types/react-dom @types/jest
  1. tsc --init
  2. 在根目录创建 tsconfig.json 文件

关于 tsconfig.json 文件的配置

  • inclued 哪些文件需要编译
 "inclued": [
     "../src/**/*"  // ** 所有目录下的,  * 所有文件  
 ]
  • exclued 哪些文件不需要被编译 (有默认值 [node_modules])
 "exclued": [
     "../src/holle/*"  // ** 所有目录下的,  * 所有文件  
 ]
  • exdents 继承一个配置文件
  • files 你要编译的文件 一个一个的列举出来
"files": [
    "holle.ts",
    "demo.ts"
]

compilerOptions

  • target ts被编译为的es版本
  • module 模块 被编译为的模块
  • lib 库 你引用了哪些库 默认就好不用动
  • outDir 输出的文件夹
  • outFile 合并成一个文件。(一般不用,这一步东西交给打包工具去做)
  • allowJs 是否对js文件进行编译 默认是false
  • checkJs 是否检查js文件
  • removeComments 是否移除注释
  • noEmit 不生成遍以后的文件
  • noEmitNoError 编译错误的时候不生成编译后的文件

ts执行的一些配置

  • strict 所有 严格检查的总开关 true 开启下列所有开关
  • alwaysStrict 是否开启严格模式
  • noImplicitAny 是否允许隐式的any
  • noImplicitThis 是否允许不明确的this
  • strictNullCheck 严格检查空值
    "compilerOptions": {
        "target": "es6",  
        "module": "es2015",
        // lib:[],
        "outDir": "./dist",
        "allowJs": true
    }