-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(types): 新增加requiredKeys用于声明对象中的某个字段为必选
- Loading branch information
1 parent
f816244
commit fc18f97
Showing
4 changed files
with
55 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
|
||
/** | ||
* | ||
* 用于获取对象 T 中指定的属性键 Keys,并将这些键对应的属性设置为必选 | ||
* | ||
* @example | ||
* 假设有一个接口 Person { | ||
* name?: string; | ||
* age?: number; | ||
* } | ||
* 我们想要创建一个新的类型,其中 name 属性是必选的: | ||
* type PersonWithRequiredName = RequiredKeys<Person, 'name'>; | ||
* 这将创建一个新的类型,等同于: | ||
* type PersonWithRequiredName = { | ||
* name: string; | ||
* age?: number; | ||
* } | ||
* | ||
* @param T 必须是一个对象类型 | ||
* @param keys 必须是 T 的键的联合类型 | ||
* | ||
*/ | ||
export type RequiredKeys<T extends object, Keys extends keyof T> = T & Required<Pick<T,Keys>> | ||
|
||
|