首页 > 解决方案 > TypeScript:typeof 泛型类型函数,无需解析为任何或未知

问题描述

我一直在尝试为此寻找解决方案,但我所有的谷歌搜索都没有成功。

我的目标是将泛型类型函数的参数类型提取到一个新的泛型类型中,以便以后使用:

// we have a given function like this:
function genericFunction<T>(a: T) {
  return a;
}

type genericParamsType = Parameters<typeof useFormOriginal>; // this will resolve to a type of [unkown]
// I would like to achieve smth like this:
// type genericParamsType<PassDownType> = Parameters<typeof useFormOriginal<PassDownType>>;
// but that is a syntax error

// if it would work, I could the following:
// const newparams: genericParamsType<string> = ["hello"] // correct
// const newparams2: genericParamsType<number> = ["hello"] // error because its not a string

标签: typescripttypestypescript-generics

解决方案


推荐阅读