Typescript ReturnType of generic function

前端 未结 4 589
长发绾君心
长发绾君心 2020-11-29 06:41

The new ReturnType in TypeScript 2.8 is a really useful feature that lets you extract the return type of a particular function.

function foo(e:          


        
4条回答
  •  情深已故
    2020-11-29 07:02

    This is my currently working solution for extracting un-exported internal types of imported libraries (like knex):

    // foo is an imported function that I have no control over
    function foo(e: T): InternalType {
        return e;
    }
    
    interface Wrapper {
      // wrapped has no explicit return type so we can infer it
      wrapped(e: T) {
        return foo(e)
      }
    }
    
    type FooInternalType = ReturnType['wrapped']>
    type Y = FooInternalType
    // Y === InternalType
    

提交回复
热议问题