-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
ReturnType<F> in parameter of generic function breaks inference when F
has one implicitly typed param
#33042
Comments
There's a reason I want to use snippet 2 and not snippet 3, For snippet 2, //Innocent enough.
//Returns a T
type Callback<T> = (...args : any[]) => T;
//Forgive the contrived example
declare function withReturnTypeInParam<
F extends Callback<3|5>,
>(
f: F & (
ReturnType<F> extends 3 ?
unknown :
[
"Only 3 allowed, received",
ReturnType<F>
]
)
): ReturnType<F>
declare function withReturnTypeInParam2<
F extends Callback<3|5>,
>(
f: F & (
ReturnType<F> extends 3 ?
unknown :
[
"Only 3 allowed, received",
ReturnType<F>
]
)
): ReturnType<F>
let x = withReturnTypeInParam;
//Expected: OK
//Actual : OK
x = withReturnTypeInParam2; For snippet 3, //Innocent enough.
//Returns a T
type Callback<T> = (...args : any[]) => T;
//Forgive the contrived example
declare function returnTypeAsTypeParam<
T extends 3|5,
>(
f: Callback<
& T
& (
T extends 3 ?
unknown :
[
"Only 3 allowed, received",
T
]
)
>
): T
declare function returnTypeAsTypeParam2<
T extends 3|5,
>(
f: Callback<
& T
& (
T extends 3 ?
unknown :
[
"Only 3 allowed, received",
T
]
)
>
): T
let x = returnTypeAsTypeParam;
/*
Expected: OK
Actual:
Type
'<T extends 3 | 5>(f: Callback<T & (T extends 3 ? unknown : ["Only 3 allowed, received", T])>) => T'
is not assignable to type
'<T extends 3 | 5>(f: Callback<T & (T extends 3 ? unknown : ["Only 3 allowed, received", T])>) => T'.
Two different types with this name exist, but they are unrelated.
*/
x = returnTypeAsTypeParam2;; The conditional type must be in the return type of |
This is working as intended. In the call The core issue here is that you can't simultaneously infer from an argument and depend on the argument. |
Doesn't What's the reason Wait- That makes sense. |
I guess I'll close this issue.
I have seen cases where TS is able to infer the type of the argument, depend on it, and also correctly infer the But it probably only worked in those cases out of dumb luck, I guess |
TypeScript Version: 3.5.1
Search Terms:
Code
This is me opening a new issue for #29133
I have three separate code snippets, please bear with me =(
Playground
Playground
Playground
Related Issues:
#29133
#32540 (comment)
I hope this is concise enough ><
I've commented all the relevant locations with what is expected and what actually happens.
Snippets 2 (
withReturnTypeInParam
) and 3 (returnTypeAsTypeParam
) are expected to have the same behaviour because they mean the same thing semantically (to me).They are just expressed differently syntactically.
I just have trouble understanding why snippet 2 breaks unless I have an explicit type annotation. I'm 99% sure it has to be a bug.
And I have trouble understanding why snippet 3 is OK
The text was updated successfully, but these errors were encountered: