在不调用函数的情况下检索函数的返回类型 [英] Retrieve return type of function w/o calling the function
本文介绍了在不调用函数的情况下检索函数的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我在 TypeScript 中有一个这样的函数:
Say I have a function like this in TypeScript:
export const foo = function(){
return {
a: 1,
b: true,
c: 'bar'
}
};
如果我将此函数导入另一个文件:
if I import this function into another file:
import {foo} from './foobar';
我的问题是 - 有没有办法获得 foo
的返回类型,而无需实际调用 foo
?
My question is - is there a way to get the return type of foo
without actually calling foo
?
推荐答案
Typescript 2.8 现在可以实现
This is now possible with Typescript 2.8
let foo = function() {
return {
a: 1,
b: true,
c: 'bar'
}
};
type ComplexObj = ReturnType<typeof foo>; // {a: number, b: boolean, c: string}
这篇关于在不调用函数的情况下检索函数的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文