-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Explain use of never[]
as most generic function argument
#2714
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
base: v2
Are you sure you want to change the base?
Conversation
For newbies like me that read the manual start-to-end to get an introduction into the concepts, the appearance of `never[]` here is surprising, inexplicable and confusing. So, I was very happy about the thorough explanation linked in this commit.
Ping - anybody out there? |
@andrewbranch Is this of interest? |
@@ -204,6 +204,8 @@ type Str = GetReturnType<(x: string) => string>; | |||
type Bools = GetReturnType<(a: boolean, b: boolean) => boolean[]>; | |||
// ^? | |||
``` | |||
If you are curious about the use of `never[]` as the most generic type for function arguments, there is an excellent explanation in this [reddit](https://www.reddit.com/r/typescript/comments/muyl55/comment/gv9ndij/?utm_source=reddit&utm_medium=web2x&context=3). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we probably need an explainer about this, but I don't think that this wording or a link to reddit is appropriate. Note that TS 5.0 greatly refined the type relationship between the different function "top types" such that this answer may not longer be accurate. Though the end result of "you probably want never[]
" may still apply.
Related PR: microsoft/TypeScript#52282
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, I think the example should replace never[]
with any[]
. We try to avoid having to include these “by the way did you know about this very confusing thing???” asides in the handbook specifically. An explainer might be better suited in the reference pages of the docs, but I don’t know if there’s any existing content where it would make sense to slot in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did some sleuthing to figure out the origin of this; it seems like this has been there since the beginning of the new handbook in microsoft/TypeScript-New-Handbook@1a21a7f; @DanielRosenwasser do you have an opinion on this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that any[]
should be sufficient for the purposes of this example, especially since we use any[]
in the definition of Flatten<T>
further up on the same page.
@@ -204,6 +204,8 @@ type Str = GetReturnType<(x: string) => string>; | |||
type Bools = GetReturnType<(a: boolean, b: boolean) => boolean[]>; | |||
// ^? | |||
``` | |||
If you are curious about the use of `never[]` as the most generic type for function arguments, there is an excellent explanation in this [reddit](https://www.reddit.com/r/typescript/comments/muyl55/comment/gv9ndij/?utm_source=reddit&utm_medium=web2x&context=3). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, I think the example should replace never[]
with any[]
. We try to avoid having to include these “by the way did you know about this very confusing thing???” asides in the handbook specifically. An explainer might be better suited in the reference pages of the docs, but I don’t know if there’s any existing content where it would make sense to slot in.
For newbies like me that read the manual start-to-end to get an introduction into the concepts, the appearance of
never[]
here is surprising, inexplicable and confusing. So, I was very happy about the thorough explanation linked in this commit.