简体   繁体   中英

How to infer return type of function with provided arguments?

Related to https://github.com/Microsoft/TypeScript/issues/26043

const f = <T extends any>(a: T) => a;

// How to implement ReturnTypeWithArguments ???

type r = ReturnTypeWithArguments<typeof f, number> // number

There is no syntax for applying type parameters to a generic function in type annotation. We can declare a function in which we invoke the generic function (which we won't use) and get the return value of that:

const f = <T extends any>(a: T) => a;

const fForNumber = () => f(0)
type r = ReturnType<typeof fForNumber> // number

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM