no-non-null-assertion
禁用使用 !
后缀运算符的非空断言。
TypeScript 的 !
非空断言运算符向类型系统声明表达式是非空的,即不是 null
或 undefined
。使用断言来告知类型系统新信息通常表明代码并非完全类型安全。通常,更好的做法是构建程序逻辑,以便 TypeScript 能够理解值何时可能为空。
无效
interface Example {
property?: string;
}
declare const example: Example;
const includes = example.property!.includes("foo");
有效
interface Example {
property?: string;
}
declare const example: Example;
const includes = example.property?.includes("foo") ?? false;