- noFallthroughCasesInSwitch">noFallthroughCasesInSwitch
- noImplicitOverride">noImplicitOverride
- noImplicitReturns">noImplicitReturns
- noPropertyAccessFromIndexSignature">noPropertyAccessFromIndexSignature
- noUncheckedIndexedAccess">noUncheckedIndexedAccess
- noUnusedLocals">noUnusedLocals
- noUnusedParameters">noUnusedParameters
noFallthroughCasesInSwitch
默认:false
报告switch语句中失败案例的错误,确保switch语句中的任何非空情况都包括break或return。
const a: number = 6;
switch (a) {
case 0:
// Fallthrough case in switch.
console.log("even");
case 1:
console.log("odd");
break;
}
noImplicitOverride
4.3 新增
当此选项被启用时,除非显式地使用override
关键字,否则重写父类中的任何方法将引起错误。
class Album {
setup() {}
}
class MLAlbum extends Album {
override setup() {}
}
class SharedAlbum extends Album {
setup() {}
// This member must have an 'override' modifier because it overrides a member in the base class 'Album'.
}
noImplicitReturns
默认:false
当启用时,TypeScript会检查函数中的所有代码路径,以确保它们返回一个值
function lookupHeadphonesManufacturer(color: "blue" | "black"): string {
// Function lacks ending return statement and return type does not include 'undefined'.
if (color === "blue") {
return "beats";
} else {
"bose";
}
}
noPropertyAccessFromIndexSignature
4.2 新增
默认:false
当开启时,通过点操作符访问的不明确属性会被警告(但是中括号语法不会被警告)。
interface GameSettings {
// Known up-front properties
speed: "fast" | "medium" | "slow";
quality: "high" | "low";
// Assume anything unknown to the interface
// is a string.
[key: string]: string;
}
const settings = getSettings();
settings.speed;
settings.quality;
// Property 'username' comes from an index signature, so it must be accessed with ['username'].
// This would need to be settings["username"];
settings.username;
noUncheckedIndexedAccess
4.1 新增
当开启时,未定义的签名访问会加上undefined类型
interface EnvironmentVars {
NAME: string;
OS: string;
// Unknown properties are covered by this index signature.
[propName: string]: string;
}
declare const env: EnvironmentVars;
// Declared as existing
const sysName = env.NAME;
const os = env.OS; // const os: string
// Not declared, but because of the index
// signature, then it is considered a string
const nodeEnv = env.NODE_ENV; // const nodeEnv: string | undefined
noUnusedLocals
默认:false
未使用的局部变量会抛错。
const createKeyboard = (modelID: number) => {
const defaultModelID = 23;
// error: 'defaultModelID' is declared but its value is never read.
return { type: "keyboard", modelID };
};
noUnusedParameters
默认:false
未使用的参数跑错。
const createDefaultKeyboard = (modelID: number) => {
// error: 'modelID' is declared but its value is never read.
const defaultModelID = 23;
return { type: "keyboard", modelID: defaultModelID };
};