deno.com

禁止非空断言

禁止使用后缀运算符 ! 进行非空断言。

TypeScript 的 ! 非空断言运算符向类型系统断言某个表达式是非空的,即不是 nullundefined。使用断言来告知类型系统新信息通常是代码未完全类型安全的标志。通常,更好的做法是组织程序逻辑,以便 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;

您找到所需内容了吗?

隐私政策