Obtaining the return type of a function

前端 未结 7 2342
既然无缘
既然无缘 2020-12-02 08:56

I have the following function:

function test(): number {
    return 42;
}

I can obtain the type of the function by using typeof

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 09:41

    The easiest way in the TypeScript 2.8:

    const foo = (): FooReturnType => {
    }
    
    type returnType = ReturnType;
    // returnType = FooReturnType
    

提交回复
热议问题