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

5 - Get Readonly Keys #33298

Open
chenghao125 opened this issue May 19, 2024 · 0 comments
Open

5 - Get Readonly Keys #33298

chenghao125 opened this issue May 19, 2024 · 0 comments
Labels
5 answer Share answers/solutions to a question en in English

Comments

@chenghao125
Copy link

  1. 首先遍历T的所有属性
  2. 判断单条属性是否是只读属性, 通过Equal<{ [k in P]: T[k]}, { -readonly [R in P]: T[R] },如果为true,表示为非只读属性,则过滤掉,如果为false,代表只读属性,添加到映射的对象中
  3. 对过滤后的对象取 keyof
type GetReadonlyKeys<T> = keyof { 
  [P in keyof T as Equal<{ [k in P]: T[k]}, { -readonly [R in P]: T[R] }> extends true ? never: P]: T[P]
}
@chenghao125 chenghao125 added answer Share answers/solutions to a question en in English labels May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

1 participant