-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Description
TypeScript Version: 3.5.1
Search Terms:
parameter, never, return type inference
Code
type NeverF = (arg : never) => void;
//"y"
type Extends = NeverF extends (...args : any) => any ?
"y" :
"n"
;
type TsReturnType<T extends (...args : any) => any> =
T extends (...args : any) => infer R ?
R :
"wtf"
;
//Expected: void
//Actual : "wtf"
type r = TsReturnType<NeverF>;
//Expected: void
//Actual : any
type r2 = ReturnType<NeverF>;
Expected behavior:
Both should resolve to void
Actual behavior:
Both resolve to the false
branch
Playground 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,
const query2 = myQuery.map((row) => {
return {
...row,
x : Math.random(),
};
});
The type of query2
should now be something like,
{
/*snip*/
mapDelegate : (row : never, connection : /*snip*/) => (
{ /*snip typeof row*/, x : number }
),
/*snip*/
}
Then,
const query3 = query2.map((row) => {
return {
...row,
x : row.x + 3.141,
y : "someNewValue",
};
});
The type of query3
should now be something like,
{
/*snip*/
mapDelegate : (row : never, connection : /*snip*/) => (
{ /*snip typeof row*/, x : number, y : string }
),
/*snip*/
}
I'm also using a never
parameter because I want to save on Emit 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,
- Only care about the return type
- Not meant to be invoked externally
- Save on emit time