-
Notifications
You must be signed in to change notification settings - Fork 12.8k
unknown[] as rest params with generic parameter breaks generic inference #27439
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
Comments
This is due to two things:
You'd be best off picking the lowest bound for the type of type ReturnOf<F> = F extends (...args: never[]) => infer U ? U : never; If #26796 were solved, this would've worked, but I still believe |
EDIT: Sorry I have successfully managed to confuse myself, feel free to ignore my question. @DanielRosenwasser So functions are contravariant in the the domain when checking conditional types, even with strictFunctionTypes off? // --strictFunctionTypes: off
declare function f<T>(x: T): T;
type ReturnOf<F> = F extends (...args: unknown[]) => infer U ? U : never;
type Test<F> = F extends (...args: unknown[]) => unknown ? unknown : never;
declare const d: ReturnOf<typeof f>; // never;
declare const e: Test<typeof f>; // unknown; What I think is happening is that first |
Hmm, I want to say you're right, but I'd like to check with @ahejlsberg or @weswigham first. |
@jack-williams @DanielRosenwasser That is indeed what is happening. |
TypeScript Version: 3.1.0
Search Terms: unknown conditional rest params
Code
Expected behavior:
d
is the same asc
(i.e. type{}
)Actual behavior:
d
is typenever
Playground Link
Related Issues: didn't see any; apologies if I missed it
Notes: this only happens if there's a
T
-typed (or union withT
) parameter. Even something likePromise<T>
seems to cause it to evaluate to{}
. I'm not even 100% sure this is a bug (I might be missing/misunderstanding something in howunknown
is handled), but it seems like in this situation it should behave the same as theany
version.The text was updated successfully, but these errors were encountered: