从泛型函数的声明中获取参数 - TypeScript 3.3
Get parameters from generic functions' declaration - TypeScript 3.3
我不知道如何获取在接口中声明的函数的参数类型。我需要对它们进行适当的类型检查。可能我需要使用:来自 TypeScript 版本的参数 class:3.3:https://github.com/Microsoft/TypeScript/blob/v3.3.1/lib/lib.es5.d.ts#L1471-L1474 但我不知道如何使用它。
interface MyFunctions {
FIRST_FUNCTION: () => void;
SECOND_FUNCTION: (string, number) => void;
}
class Params<Functions> {
private functions: Map<keyof Functions, Set<Functions[keyof Functions]>> = new Map();
// some other functions...
public boom<K extends keyof Functions>(func: K, ...args: ???? /* here I don't know how to define type*/ ) {
this.functions.get(func)(args);
}
}
这是我解决您的问题的方法,请注意我必须清理很多东西并猜测您在做什么:
interface MyFunctions {
FIRST_FUNCTION: () => void;
SECOND_FUNCTION: (x: string, y: number) => void; // FIXED
}
// constrain Functions to a type holding only function properties
class Params<Functions extends Record<keyof Functions, (...args: any[]) => any>> {
private functions: Map<keyof Functions, Set<Functions[keyof Functions]>> = new Map();
// use Parameters as requested
public boom<K extends keyof Functions>(func: K, ...args: Parameters<Functions[K]>) {
// assert that it returns a set of the right kind of function
const funcSet = (this.functions.get(func) || new Set()) as Set<Functions[K]>;
// okay, and remember to use spread
funcSet.forEach(f => f(...args));
}
}
new Params<{a: string}>(); // error, string is not a function
new Params<MyFunctions>().boom("FIRST_FUNCTION"); // okay
new Params<MyFunctions>().boom("SECOND_FUNCTION", "a", 1); // okay
与您的问题相关的部分:
I constrained 泛型 Functions
类型为 Record<keyof Functions, (...args: any[]) => any>
以便编译器知道 Functions
的所有属性必须是函数。这将阻止您调用 new Params<{a: string}>()
.
我将 args
剩余参数键入 Parameters<Functions[K]>
,其中 Functions[K]
looks up [=11= 的 属性 ] 键 K
。因为,由于泛型约束,编译器知道 Functions[K]
必须是函数类型,所以很乐意允许您将它传递给 Parameters<>
和 return 参数元组。
我重新编写了 boom()
的实现,以便对我来说更有意义。我需要对参数做一个 type assertion to convince the compiler that what comes out of this.functions.get(func)
is actually a set of Functions[typeof func]
as opposed to a set of the wider Functions[keyof Functions]
. And I called each function element of the acquired Set
, using spread syntax。如果这些假设是错误的,希望它们仍能引导您朝着有用的方向前进。
希望对您有所帮助;祝你好运!
我不知道如何获取在接口中声明的函数的参数类型。我需要对它们进行适当的类型检查。可能我需要使用:来自 TypeScript 版本的参数 class:3.3:https://github.com/Microsoft/TypeScript/blob/v3.3.1/lib/lib.es5.d.ts#L1471-L1474 但我不知道如何使用它。
interface MyFunctions {
FIRST_FUNCTION: () => void;
SECOND_FUNCTION: (string, number) => void;
}
class Params<Functions> {
private functions: Map<keyof Functions, Set<Functions[keyof Functions]>> = new Map();
// some other functions...
public boom<K extends keyof Functions>(func: K, ...args: ???? /* here I don't know how to define type*/ ) {
this.functions.get(func)(args);
}
}
这是我解决您的问题的方法,请注意我必须清理很多东西并猜测您在做什么:
interface MyFunctions {
FIRST_FUNCTION: () => void;
SECOND_FUNCTION: (x: string, y: number) => void; // FIXED
}
// constrain Functions to a type holding only function properties
class Params<Functions extends Record<keyof Functions, (...args: any[]) => any>> {
private functions: Map<keyof Functions, Set<Functions[keyof Functions]>> = new Map();
// use Parameters as requested
public boom<K extends keyof Functions>(func: K, ...args: Parameters<Functions[K]>) {
// assert that it returns a set of the right kind of function
const funcSet = (this.functions.get(func) || new Set()) as Set<Functions[K]>;
// okay, and remember to use spread
funcSet.forEach(f => f(...args));
}
}
new Params<{a: string}>(); // error, string is not a function
new Params<MyFunctions>().boom("FIRST_FUNCTION"); // okay
new Params<MyFunctions>().boom("SECOND_FUNCTION", "a", 1); // okay
与您的问题相关的部分:
I constrained 泛型
Functions
类型为Record<keyof Functions, (...args: any[]) => any>
以便编译器知道Functions
的所有属性必须是函数。这将阻止您调用new Params<{a: string}>()
.我将
args
剩余参数键入Parameters<Functions[K]>
,其中Functions[K]
looks up [=11= 的 属性 ] 键K
。因为,由于泛型约束,编译器知道Functions[K]
必须是函数类型,所以很乐意允许您将它传递给Parameters<>
和 return 参数元组。
我重新编写了 boom()
的实现,以便对我来说更有意义。我需要对参数做一个 type assertion to convince the compiler that what comes out of this.functions.get(func)
is actually a set of Functions[typeof func]
as opposed to a set of the wider Functions[keyof Functions]
. And I called each function element of the acquired Set
, using spread syntax。如果这些假设是错误的,希望它们仍能引导您朝着有用的方向前进。
希望对您有所帮助;祝你好运!