繁体   English   中英

如何用提供的参数推断函数的返回类型?

[英]How to infer return type of function with provided arguments?

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

在类型注释中没有将类型参数应用于通用函数的语法。 我们可以声明一个函数,在其中调用通用函数(我们将不使用)并获取该函数的返回值:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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