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

1097 - IsUnion #26845

Open
smileboyi opened this issue Apr 25, 2023 · 0 comments
Open

1097 - IsUnion #26845

smileboyi opened this issue Apr 25, 2023 · 0 comments
Labels
1097 answer Share answers/solutions to a question en in English

Comments

@smileboyi
Copy link

smileboyi commented Apr 25, 2023

type IsUnion<T, U = T> = (T extends T ? U extends T ? true : unknown : never) extends true ? false : true

思路:

  1. T extends T可以判断T是否为never;
  2. U extends T,extends可以拆分联合类型分别判断,再将每个子结果再联合起来;
  3. 如果T是联合类型,那么子结果就会含有unknown ,括号最终的结果是一个含有unknown的联合类型;
  4. Union extends true,Union含有unknown就是一个联合类型,只含有never或true就不是一个联合类型,可以通过extends true来判断。
@smileboyi smileboyi added answer Share answers/solutions to a question en in English labels Apr 25, 2023
@github-actions github-actions bot added the 1097 label Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1097 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

1 participant