TypeScript 最全tsconfig.json系列之compilerOptions中类型检查配置项

1,061 阅读4分钟

*一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第3天,点击查看活动详情

compilerOptions

TypeScript的编译设置选项,里面包含一系列的编译配置,也是tsconfig.json中的核心知识点。下面会详细讲解所有的配置项,目前先以其中的类型检查配置项开始。

strict

布尔值,用于指定是否启动所有类型检查,如果设为true则会同时开启这个下面几个属性设置的严格类型检查,默认为false。

noImplicitAny

布尔值,编译器会默认为没有设置明确类型的值为其设置为any类型,如果noImplicitAny的值为true的话,则没有明确的类型会报错, 默认值为false。如下

function test(a) { //错误 noImplicitAny为true 编译器不会设置any类型,故a未知类型
    return a;
}

strictNullChecks

布尔值,如果为true, null和undefined值不能赋给不是这两种类型的值,别的类型也不能赋给他们,除了any类型。还有个例外就是undefined可以赋值给void类型, 默认值为false。

// 默认情况下strictNullChecks为false
let a1: number;
a1 = 1;      //成功
a1 = undefined; //成功
a1 = null;       //成功
let a2: any = undefined //成功
let a3: void = undefined //成功

// strictNullChecks为true
let a1: number;
a1 = 1;      //成功
a1 = undefined; //错误
a1 = null;       //错误
let a2: any = undefined //成功
let a3: void = undefined //成功

strictFunctionTypes

布尔值,用于指定是否使用函数参数双向协变检查, 默认值为false。比如如下场景

class Person {}
class Teacher extends Person {
  say(){}
}
class Student extends Person {
  say(){}
}
function personSay(say: (person: Person) => void) {
  const student = new Student()
  say(student)
}
function teacherSay(teacher: Teacher): void {
   teacher.say();
}
personSay(teacherSay); // strictFunctionTypes为true时,会检测出此处传参问题

strictBindCallApply

布尔值,如果为true, 会对bind、call和apply绑定的方法的参数的检测是严格检测的, 默认值为false, 如下图

image.png

strictPropertyInitialization

布尔值,如果为true, 会检查类中不是undefined的属性是否已经在构造函数里初始化,如果要开启这项,需要同时开启strictNullChecks,默认为false。

image.png

noImplicitThis

布尔值,如果为true,当this表达式的值为any类型的时候,生成一个错误,默认为false。

image.png

useUnknownInCatchVariables

布尔值,Catch 语句中默认使用 unknown 类型而不是any
默认情况下是any
image.png
useUnknownInCatchVariables设置为true后时unknown image.png

alwaysStrict

布尔值,如果为true, 指定始终以严格模式检查每个模块,并且在编译之后的js文件中加入"use strict"字符串,用来告诉浏览器该js为严格模式,默认为false。

noUnusedLocals

布尔值,如果为true, 检查是否有定义了但是没有使用的变量,会在变量项出现黄色波浪警告,默认值为false。这个也可以配合eslint来做检查。

image.png

noUnusedParameters

布尔值,如果为true, 检查是否有在函数体中没有使用的参数,默认为false。和noUnusedLocals的效果一样。

function test(name: string, age: number) { // 会检测处name没有使用,警告提示。
  console.log(age);
}

exactOptionalPropertyTypes

布尔值,如果为true, 可选属性类型?:应完全按照编写的方式来解释,undefined 不会添加到类型中。如下

interface Person {
    name?: string,   // 错误写法
    name1: string | undefined   // 正确写法
}
const xiaoming: Person = {
    name: undefined // 错误 不能将undefined赋值给name
    name1: undefined // 正确
}

此属性设置为true的前提条件是strictNullChecks设置为true。

noImplicitReturns

布尔值,如果为true, 检查设置返回值类型的函数的所有返回路径中是否都有返回值,如果其中一条路径没有返回值,则会咋在定义返回值类型处提出警告,默认值为false。

const flag:boolean = true;
function test(): string { //在string会提示“并非所有代码路径都返回值”
  if (flag) {
    return 'hello';
  }
}

noFallthroughCasesInSwitch

布尔值,如果为true, 检查switch中是否有case中没有使用break跳出switch,默认为false。

const type: number = 1
function test() {
    switch (type) {
        case 1:
            break;
        case 2:
            console.log('1'); // 如果设置为true,此种情况会提示警告
        default:
            break;
    }
}

noUncheckedIndexedAccess

布尔值,如果为true,并且strict设置为true时, 使用索引访问某类型属性时, 该类型属性会被加上 undefined 类型,默认:false。

image.png

noImplicitOverride

布尔值,如果为true,检查子类继承自基类时,其重载的函数命名与基类的函数不同步问题,就是说子类重写父类中的函数时必须在函数前面添加override修饰符,不是重装的不要加override, 默认:false。 image.png

noPropertyAccessFromIndexSignature

布尔值,如果为true,不允许使用点语法来访问未定义的字段,应该用中括号的形式访问data['name'],默认:false。

interface Person {
  name: string;
  [x: string]: any;
}

const data: Person = { name: '小明', age: 18 }; 
console.log(data.age); // 错误
console.log(data['age']) // 正确

allowUnreachableCode

布尔值,是否允许无法访问的代码,默认值为true。

  • true:未使用的标签被忽略,不会出现警告。
  • false:未使用的代码段会出现警告。 image.png

allowUnusedLabels

布尔值,是否允许无未使用的标签,默认值为true。

  • true:未使用的标签被忽略,不会出现警告。
  • false:引发有关未使用标签会出现警告。