-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
I'm trying to create something similar to native ReturnType type. In comparison to the later, it should also consider generic types of arguments.
I guess the idea will be clear from the code below.
If it can be done now - I'd be happy to receive some links. Otherways it'd be great to know if there are any plans to cover that behavior. In case it could be achieved by another not-implemented feature I'll be glad for the link to an issue to be able to track its progress.
TypeScript Version: 2.9.2
Search Terms: infer function return type
Code
// it works with actual code:
function identity<T>(a: T) {
return a;
}
const one = identity(1); // typeof one === 1 # everything is great
function call<A, R>(arg: A, fn: Func<A, R>): R {
return fn(arg);
}
const two = call(2 as 2, i => i) // typeof two === 2 # everything is great
// but won't work with types
type Func<A, R> = (arg: A) => R;
// ReturnTypeByArg is inspired by `ReturnType` from TS 2.8.*
type ReturnTypeByArg<T, F extends Func<T, any>> = F extends Func<T, infer R> ? R : any;
type Identity = <T>(a: T) => T;
type Result = ReturnTypeByArg<4, Identity>; // Result === {} # not what I expectExpected behavior:
I expect TS to be able to infer such return type
Actual behavior:
Return type is {}
Playground Link: link
Related Issues:
hallettj, shmidt-i, csr632, magicgrl111, btoo and 6 more
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created