JavaScript/typescript/Partial:修订间差异
来自康健生活
创建页面,内容为“== 标题文字 == <blockquote> <syntaxhighlight lang="javascript"> type Partial<T> = { [P in keyof T]?: T[P]; }; </syntaxhighlight></blockquote>” |
|||
| 第1行: | 第1行: | ||
== | == 引言 == | ||
将传入的属性变为可选。 | |||
<blockquote> | <blockquote> | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| 第6行: | 第8行: | ||
}; | }; | ||
</syntaxhighlight></blockquote> | </syntaxhighlight></blockquote> | ||
== 示例 == | |||
<syntaxhighlight lang="javascript"> | |||
// 原始类型 | |||
interface Engineer{ | |||
name: string; | |||
age: number; | |||
profession: string; | |||
interest: string[]; | |||
} | |||
type BackendEngineer = Partial<Engineer>; | |||
// 等价于 | |||
// BackendEngineer === { | |||
name?: string; | |||
age?: number; | |||
profession?: string; | |||
interest?: string[]; | |||
} | |||
</syntaxhighlight> | |||