Why do I have to type the input as `never` in a generic function?

68 Views Asked by At

When I want to represent a type for *some* function, an intuitive way would be to say that it accepts an unknown argument (let's agree that all functions accept one argument):

type SomeFunction = (input: unknown) => unknown

… however, this leads to an error: Type 'unknown' is not assignable to type '<the type of the argument>'.

But if I use never instead of unknown, everything works fine:

type SomeFunction = (input: never) => unknown

Try it.

I always thought of never to be the type of no value (one that I'd use to type the return value of a function that does not have a reachable return path). But so does unknown represent anything – and thus nothing.

What makes never work better than unknown in this case?

0

There are 0 best solutions below