ts编译选项文档

当 js 代码中写了 import 后,会自动进入严格模式。

1. include

包含

    • 代表匹配文件
  • ** 代表匹配任意目录


2. exclude

不包含,不需要编译

3. compilerOptions

编译器的选项

  • target

指定 ts 被编译为 es 的版本

  • module

指定模块化的规范

  • outDir

指定编译后文件存放的目录

  • noEmit

只想利用 ts 进行语法检查,而不生成编译后文件,推荐设置此属性


以下为编辑器限制


  • noImplicitAny

true:不允许隐式any

  • noImplicitThis
    true:不允许不明确类型的this
  • strictNullChecks

严格检查空值

n. 其他

判断一个值是否为 null 的实用方法

  1. let test = document.getElementById('box1');
  2. if (test !== null) {
  3. test.addEventListener('click', function() {alert('click');});
  4. }
  5. // OR 更简便的方法
  6. test?.addEventListener('click', function() {alert('click');});