Typescript 是否可以为多类型参数类/函数的多个参数创建类型别名?

Typescript 是否可以为多类型参数类/函数的多个参数创建类型别名?,typescript,vue.js,typescript-generics,Typescript,Vue.js,Typescript Generics,在许多情况下(特别是Vue),泛型函数需要3、4甚至5个类型参数。我想知道是否有一种方法可以为参数列表创建一个类型别名,这样我就不必每次都写出来。我在想象这样的事情: // Generic function with multiple type parameters function verboseGenericFunc<T, U, V extends T>(thing: T): V { // Do stuff with T, U, V return thing as V;

在许多情况下(特别是Vue),泛型函数需要3、4甚至5个类型参数。我想知道是否有一种方法可以为参数列表创建一个类型别名,这样我就不必每次都写出来。我在想象这样的事情:

// Generic function with multiple type parameters
function verboseGenericFunc<T, U, V extends T>(thing: T): V {
  // Do stuff with T, U, V
  return thing as V;
};

// Alias for the tuple of types implementing the required parameter interfaces
type VerboseTypeParams = [ConcreteT, ConcreteU, ConcreteV];

// Spread the alias to satisfy all the type parameters
const v = verboseGenericFunc<...VerboseTypeParams>(thing);
//具有多个类型参数的泛型函数
函数verboseGenericFunc(thing:T):V{
//用T,U,V做一些事情
把事物归为V;
};
//实现所需参数接口的类型元组的别名
输入VerboseTypeParams=[concrete,concretu,ConcreteV];
//展开别名以满足所有类型参数
const v=verboseGenericFunc(事物);

您可以创建一个类型对象,该对象是传递到模板化函数中的类型参数的别名。如果在创建类型别名时传入了错误的geenric类型,则其计算结果为undefined

 type VerboseTypeParams<T,U,V> = V extends T? {"T":T, "U":U, "V":V}: undefined

 type concrete = VerboseTypeParams<string,number, string>; //evaluates to type we want
type VerboseTypeParams=V扩展T?{T:T,U:U,V:V}:未定义
具体类型=详细类型参数//计算为我们想要的类型
然后,可以在包装器函数中从类型别名对象中提取类型

// Generic function wrapper
function verboseGenericFunc(thing: concrete["T"] ):concrete["V"] {
  return verboseGenericFuncVue<concrete["T"],concrete["U"],concrete["V"]>(thing);
};
//通用函数包装器
函数verboseGenericFunc(thing:concrete[“T”]):concrete[“V”]{
返回verboseGenericFuncVue(thing);
};
包装器函数调用您想要的实际函数,调用时不需要接收任何类型参数。如果创建不兼容的包装或将错误的类型传递给正确的包装函数,typescript将发出抱怨

// Generic function with multiple type parameters, from Vue for example
function verboseGenericFuncVue<T, U, V extends T>(thing: T): V {
  // Do stuff with T, U, V
  return thing as V;
};

type concreteBad = VerboseTypeParams<string,number, number>; //evaluates to undefined

// Bad Generic function wrapper, Typescript complains on the key lookups since concrete bad is undefined
function verboseGenericFuncBad(thing: concreteBad["T"] ):concreteBad["V"] {
  return verboseGenericFuncVue<concreteBad["T"],concreteBad["U"],concreteBad["V"]>(thing);
};

const v1 = verboseGenericFunc("1"); //correct call to wrapped function, no types passed
const v2 = verboseGenericFunc(1); //typescript complains since you're not passing in a string as expected by concrete
//具有多个类型参数的泛型函数,例如来自Vue
函数verboseGenericFuncVue(thing:T):V{
//用T,U,V做一些事情
把事物归为V;
};
类型concreteBad=详细类型参数//计算结果为未定义
//错误的泛型函数包装器,Typescript在键查找时抱怨,因为具体的错误未定义
函数verboseGenericFuncBad(thing:concreteBad[“T”]):concreteBad[“V”]{
返回verboseGenericFuncVue(thing);
};
const v1=verboseGenericFunc(“1”)//正确调用包装函数,未传递任何类型
const v2=verboseGenericFunc(1)//typescript会抱怨,因为您没有按照concrete的预期传入字符串

与MacD的答案相同,但执行略有不同:只需使用helper类型作为函数的类型参数

type TypeInfo<T = any, U = any, V extends T = any> = { T: T, U: U, V: V };

// Generic function with multiple type parameters
function verboseGenericFunc<TI extends TypeInfo>(thing: TI['T']): TI['V'] {
  // Do stuff with T, U, V
  return thing as TI['V'];
};

// Alias for the tuple of types implementing the required parameter interfaces
type VerboseTypeParams = TypeInfo<number, string, 5>;

// Spread the alias to satisfy all the type parameters
const v: 5 = verboseGenericFunc<VerboseTypeParams>(12020);

// One-off type.
const w: 'hi' = verboseGenericFunc<TypeInfo<string, null, 'hi'>>('hello');
TypeInfo={T:T,U:U,V:V};
//具有多个类型参数的泛型函数
函数verboseGenericFunc(thing:TI['T']):TI['V']{
//用T,U,V做一些事情
将事物返回为TI['V'];
};
//实现所需参数接口的类型元组的别名
输入VerboseTypeParams=TypeInfo;
//展开别名以满足所有类型参数
const v:5=verboseGenericFunc(12020);
//一次性的。
常量w:'hi'=verboseGenericFunc('hello');

恐怕这样的事情根本不存在