Skip to content

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

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Copy link
Member

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

Copy link
Member

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.

Copy link
Member

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?

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.



When inferring from a type with multiple call signatures (such as the type of an overloaded function), inferences are made from the _last_ signature (which, presumably, is the most permissive catch-all case). It is not possible to perform overload resolution based on a list of argument types.

Expand Down