noFallthroughCasesInSwitch

默认:false
报告switch语句中失败案例的错误,确保switch语句中的任何非空情况都包括break或return。

  1. const a: number = 6;
  2. switch (a) {
  3. case 0:
  4. // Fallthrough case in switch.
  5. console.log("even");
  6. case 1:
  7. console.log("odd");
  8. break;
  9. }

noImplicitOverride

4.3 新增
当此选项被启用时,除非显式地使用override关键字,否则重写父类中的任何方法将引起错误。

  1. class Album {
  2. setup() {}
  3. }
  4. class MLAlbum extends Album {
  5. override setup() {}
  6. }
  7. class SharedAlbum extends Album {
  8. setup() {}
  9. // This member must have an 'override' modifier because it overrides a member in the base class 'Album'.
  10. }

noImplicitReturns

默认:false
当启用时,TypeScript会检查函数中的所有代码路径,以确保它们返回一个值

  1. function lookupHeadphonesManufacturer(color: "blue" | "black"): string {
  2. // Function lacks ending return statement and return type does not include 'undefined'.
  3. if (color === "blue") {
  4. return "beats";
  5. } else {
  6. "bose";
  7. }
  8. }

noPropertyAccessFromIndexSignature

4.2 新增
默认:false
当开启时,通过点操作符访问的不明确属性会被警告(但是中括号语法不会被警告)。

  1. interface GameSettings {
  2. // Known up-front properties
  3. speed: "fast" | "medium" | "slow";
  4. quality: "high" | "low";
  5. // Assume anything unknown to the interface
  6. // is a string.
  7. [key: string]: string;
  8. }
  9. const settings = getSettings();
  10. settings.speed;
  11. settings.quality;
  12. // Property 'username' comes from an index signature, so it must be accessed with ['username'].
  13. // This would need to be settings["username"];
  14. settings.username;

noUncheckedIndexedAccess

4.1 新增
当开启时,未定义的签名访问会加上undefined类型

  1. interface EnvironmentVars {
  2. NAME: string;
  3. OS: string;
  4. // Unknown properties are covered by this index signature.
  5. [propName: string]: string;
  6. }
  7. declare const env: EnvironmentVars;
  8. // Declared as existing
  9. const sysName = env.NAME;
  10. const os = env.OS; // const os: string
  11. // Not declared, but because of the index
  12. // signature, then it is considered a string
  13. const nodeEnv = env.NODE_ENV; // const nodeEnv: string | undefined

noUnusedLocals

默认:false
未使用的局部变量会抛错。

  1. const createKeyboard = (modelID: number) => {
  2. const defaultModelID = 23;
  3. // error: 'defaultModelID' is declared but its value is never read.
  4. return { type: "keyboard", modelID };
  5. };

noUnusedParameters

默认:false
未使用的参数跑错。

  1. const createDefaultKeyboard = (modelID: number) => {
  2. // error: 'modelID' is declared but its value is never read.
  3. const defaultModelID = 23;
  4. return { type: "keyboard", modelID: defaultModelID };
  5. };