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<> broken with never in parameter #33457
Comments
My workaround at the moment, type BetterReturnType<T extends (...args : never) => any> =
T extends (...args : never) => infer R ?
R :
never
; type NeverF = (arg : never, arg1 : any, arg2 : never) => void;
//"y"
type Extends = NeverF extends (...args : any) => any ?
"y" :
"n"
;
//Expected: void
//Actual : any
type existingReturnType = ReturnType<NeverF>;
type BetterReturnType<T extends (...args : never) => any> =
T extends (...args : never) => infer R ?
R :
never
;
//Expected: void
//Actual : void
type r = BetterReturnType<NeverF>;
//Expected: number
//Actual : number
type r2 = ReturnType<typeof Math.abs>
//Expected: string[]
//Actual : string[]
type r3 = ReturnType<(string[])["concat"]> |
never
cases return type inference to failnever
causes return type inference to fail
I think this one is subtle, and I'm not sure I have this right, but: The type The problem is that it's not true that all signatures would be related to an any signature if you did the check structurally, specifically in the case where you use The type Things appear to work OK for Here is a small explanation: type NeverF = (arg : never) => void;
type AnySignature = (...args: any) => any;
type NotAnySignature = (...args: any) => unknown;
declare const neverF: NeverF;
let anySignature: AnySignature = neverF; // ok
let notAnySignature: NotAnySignature = neverF; // error, arguments are not related. Changing the return type causes a type error to appear in the parameters because changing the return type no longer identifiers the signature as an any signature. |
@jack-williams is correct. |
You're saying Maybe I should have renamed the issue to " I'm more interested in changing the implementation of
I'm not a fan of that behavior but I'm not too emotionally invested in getting that changed. It would have been nice to know that I'm not allowed to use Instead, it was allowed, but failed to work properly (from my pov, even though it's marked as working as intended) |
Also, just so I'm clear about the "intended" behavior of
It behaves differently depending on whether it's used to constrain a type parameter, or used in a conditional type, right? Another reason I use Similar to why people would use Existential types would be nice =x |
No it behaves the same. The difference is that in your conditional type the inference of All signatures are related to the any signature, independently of where that question is asked in the checker. It seems to me that if |
Was there a reason to make the false branch return |
I tried it. I tried it on the playground, /**
* Notice the `extends (...args: any` part in the constraint.
* And the `extends (...args: never` part in the conditional type.
*
* This is intentional.
*/
type BetterReturnType<T extends (...args: any) => any> =
T extends (...args: never) => infer R ? R : any;
type T19<T extends any[]> = BetterReturnType<(x: string, ...args: T) => T[]>;
//type x = number[][]
type x = T19<number[]> So, It seems like the |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
never
causes return type inference to fail
This was marked as "working as intended" but I don't think it should be intended that "never" in a parameter breaks ReturnType<> |
@RyanCavanaugh could you take another look at this please? =( |
TypeScript Version: 3.5.1
Search Terms:
parameter, never, return type inference
Code
Expected behavior:
Both should resolve to
void
Actual behavior:
Both resolve to the
false
branchPlayground Link: Playground
Related Issues:
Did a search for "parameter never in:title" and found nothing
I'm using a function with a parameter of type
never
because I only care about the return type. And I do not want the user to invoke the function explicitly.Given the following,
The type of
query2
should now be something like,Then,
The type of
query3
should now be something like,I'm also using a
never
parameter because I want to save onEmit time
. It sounds funny but literally every bit of text I shave off helps with compile times.For large queries, the type of the
row
parameter could be huuuuuuggeeeee. Like, 100+ lines long.So,
The text was updated successfully, but these errors were encountered: