Coming soon - Get a detailed view of why an account is flagged as spam!
view details
3
Conditional type with never for function parameters
Post Body

Playground

I have a function signature like this: const func = <TParams extends Record<string, any> = never> (...args: TParams extends never ? never[] : [TParams]) => {}

In my mind, this means that if no type is provided, func can be called without args, and if there is a type, func must be called with an arg of the corresponding type.

However, the type checker always assigns never (not never[]) to args if no type is given. This wasn't an issue before TS 5.0, but since the switch, I've been getting this error:

Argument of type '[]' is not assignable to parameter of type 'never'.

If the conditional parses correctly, it should have returned args as never[], but it doesn't. Am I understanding the never type wrong?

Author
Account Strength
90%
Account Age
8 years
Verified Email
Yes
Verified Flair
No
Total Karma
1,041
Link Karma
556
Comment Karma
485
Profile updated: 5 days ago
Posts updated: 4 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
1 year ago