Is there ArgumentsType like ReturnType in Typescript?

前端 未结 6 698
一个人的身影
一个人的身影 2020-11-30 12:34

ReturnType extracts return type of a function.

Is there a way to define ArgumentsType that extracts parameter types of a

6条回答
  •  眼角桃花
    2020-11-30 13:13

    Expanding on @Titian Cernicova-Dragomir's answer, if you are using named function expressions, thus not being able to define a type, you can access the parameters with typeof like so:

    function updateTable(
      diff: Diff,
      cache: { [id: string]: Table> & IHasOBJECT_ID & IHasCONFLICTS & IHasOPTIONS },
    ) {
      // Use typeof to get the parameter type for cache
      const cacheItem: Parameters[1] = cache[diff.id];
    
      // ...
    }
    

提交回复
热议问题