Obtaining the return type of a function

前端 未结 7 2308
既然无缘
既然无缘 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条回答
  • 2020-12-02 09:47

    The code below works without executing the function. It's from the react-redux-typescript library (https://github.com/alexzywiak/react-redux-typescript/blob/master/utils/redux/typeUtils.ts)

    interface Func<T> {
        ([...args]: any, args2?: any): T;
    }
    export function returnType<T>(func: Func<T>) {
        return {} as T;
    }
    
    
    function mapDispatchToProps(dispatch: RootDispatch, props:OwnProps) {
      return {
        onFinished() {
          dispatch(action(props.id));
        }
      }
    }
    
    const dispatchGeneric = returnType(mapDispatchToProps);
    type DispatchProps = typeof dispatchGeneric;
    
    0 讨论(0)
提交回复
热议问题