Retrieve return type of function w/o calling the function

独自空忆成欢 提交于 2019-12-11 02:25:26

问题


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';

My question is - is there a way to get the return type of foo without actually calling foo?


回答1:


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}


来源:https://stackoverflow.com/questions/50263415/retrieve-return-type-of-function-w-o-calling-the-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!