Skip to content
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

实现 Partial 类型, 将 T 类型所有属性改为可选 #22

Open
keqing77 opened this issue Mar 7, 2024 · 1 comment
Open

实现 Partial 类型, 将 T 类型所有属性改为可选 #22

keqing77 opened this issue Mar 7, 2024 · 1 comment
Labels
TypeScript TypeScript 手撕代码 手撕代码

Comments

@keqing77
Copy link
Owner

keqing77 commented Mar 7, 2024

type Person = {
  name: string;
  age: number;
}

// 使用Partial将 Person 中所有的参数变为可选
type PartialPerson = Partial<Person>;

type PartialPerson = {
  name?: string | undefined;
  age?: number | undefined;
}
@keqing77 keqing77 added 手撕代码 手撕代码 TypeScript TypeScript labels Mar 7, 2024
@keqing77
Copy link
Owner Author

keqing77 commented Mar 7, 2024

type MyPartial<T> = {
  [K in keyof T]?:T[K]
}
// 遍历 T 类型的所有键, 加上可选修饰符 `?`, 值为原来的类型 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TypeScript TypeScript 手撕代码 手撕代码
Projects
None yet
Development

No branches or pull requests

1 participant