首页 > 解决方案 > 使用来自函数参数的泛型动态类型

问题描述

示例代码

interface E<T extends string> {
  operation: T
}

const opt = <T extends string>(op: T) => {
  const res: E<op> = someFunc(op)
}

function someFunc<T extends string>(op: T): E<T> {
  return {
    operation: op
  }
}

在这里,我传入op我的opt函数,然后调用someFunc.

错误:'op' refers to a value, but is being used as a type here. Did you mean 'typeof op'?ts(2749)

我的问题是:

Is it possible to pass a type of generic function as a parameter variable and use?

标签: typescript

解决方案


推荐阅读