We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
题目链接:MyOmit
实现一个 MyOmit 满足测试用例 cases
MyOmit
cases
Expected1
title
completed
Expected2
type cases = [ Expect<Equal<Expected1, MyOmit<Todo, "description">>>, Expect<Equal<Expected2, MyOmit<Todo, "description" | "completed">>> ]; // @ts-expect-error type error = MyOmit<Todo, "description" | "invalid">; interface Todo { title: string; description: string; completed: boolean; } interface Expected1 { title: string; completed: boolean; } interface Expected2 { title: string; }
type MyOmit<Type, Keys extends keyof Type> = Pick< Type, Exclude<keyof Type, Keys> >;
使用 Pick 和 Exclude 可以实现 Omit
Pick
Exclude
Omit
Exclude<keyof Type, Keys>
Type
key
Pick<Type, Exclude<keyof Type, Keys>>
1
type MyOmit<Type, Keys extends keyof Type> = { [key in Exclude<keyof Type, Keys>]: Type[key]; };
使用 Exclude 可以实现 Omit
Keys
in
Type[key]
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目
题目链接:MyOmit
实现一个
MyOmit
满足测试用例cases
Expected1
满足两个属性:title
和completed
Expected2
满足一个属性:title
答案
方法一
解析
使用
Pick
和Exclude
可以实现Omit
Exclude<keyof Type, Keys>
使用Exclude
剔除掉Type
中的key
Pick<Type, Exclude<keyof Type, Keys>>
使用Pick
挑选出1
中的结果方法二
解析
使用
Exclude
可以实现Omit
Exclude<keyof Type, Keys>
使用Exclude
剔除掉Type
中的Keys
in
遍历1
中的结果Type[key]
基础知识
The text was updated successfully, but these errors were encountered: