代码点读机 / TypeScript / Required

Required TypeScript · 工具类型

把对象里所有可选字段强制变成必填,像把"可留空"全部打上星号。

怎么用

Required<类型>

小例子

interface 配置 {
  路径?: string;
}
let c: Required<配置> = {};

类型检查结果

缺少属性"路径"

提醒:是 Partial 的反面

官方文档:https://www.typescriptlang.org/docs/handbook/utility-types.html#requiredtype

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← TypeScript全部词条