-
Notifications
You must be signed in to change notification settings - Fork 391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
「重学TS 2.0 」TS 练习题第四十六题 #67
Comments
interface Person {
name: string;
age?: number;
gender?: number;
}
type RequireAllOrNone<T, K extends keyof T> = Omit<T, K> & (Required<Pick<T, K>> | Partial<Record<K, never>>)
const p1: RequireAllOrNone<Person, "age" | "gender"> = {
name: "lolo"
};
const p2: RequireAllOrNone<Person, "age" | "gender"> = {
name: "lolo",
age: 7,
gender: 1
};
const p3: RequireAllOrNone<Person, "age" | "gender"> = {
// const p3: SetRequired<Person, "age" | "gender"> = {
// const p3: Omit<Person, "age" | "gender"> = {
name: "lolo",
age: 7,
}; 思路: 巧妙利用 never 和 可选。 |
秒啊,但是有个问题啊,partial会把值变成 never | undefined , 最终就是undefined。因此{ |
type RequireAllOrNone<T, K extends keyof T> = Omit<T, K> & ({ |
|
type RequireAllOrNone<T, K extends keyof T> =
| (Omit<T, K> & Required<Pick<T, K>>)
| { [Key in keyof T]: Key extends K ? never: T[Key] }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
实现
RequireAllOrNone
工具类型,用于满足以下功能。即当设置age
属性时,gender
属性也会变成必填。具体的使用示例如下所示:The text was updated successfully, but these errors were encountered: