【问题标题】:Get params as a type in Typescript?在 Typescript 中获取参数作为类型?
【发布时间】:2019-01-28 23:36:39
【问题描述】:

如果不使用objectinterface 或完整的函数定义,函数的参数是否可以在Typescript 的其他地方用作类型,就像ReturnType<T> 在其他地方使用返回类型作为类型定义一样?我想像 InputTypeParamType 这样的用法。

出于上下文目的,请举以下示例:

const x = (y: string, z: string) => ({[y+z]: 3})

要使用该函数的结果 ({string: number}) 作为类型,我们可以使用 ReturnType<typeof x>,但是输入参数可以用作对象类型 ({y: string, z: string}),还是 JS 的 @987654321 的限制@结构([string, string])与这个概念冲突?用例是用于向下传递参数的容器。

【问题讨论】:

    标签: javascript typescript types


    【解决方案1】:

    TypeScript 3.1 引入了名为 Parameters 的类型,正是这样做的。

    type MyParams = Parameters<typeof x> // [string, string]
    

    【讨论】:

      猜你喜欢
      • 2019-12-22
      • 2021-07-10
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 2020-01-19
      • 1970-01-01
      • 1970-01-01
      • 2020-11-24
      相关资源
      最近更新 更多