typescript - Typescript:通过索引对类函数进行类型化访问
问题描述
我需要根据ExampleOne
类的字段名称调用类的函数,DataValues
从那里获取值并将它们传递给类实例的ExampleOne
函数
告诉我如何通过索引器正确编写函数调用,如您所见,类型存在一些问题
class ExampleOne {
f1(argStr: string): string {
return `some text ${argStr}`;
}
f2(argNumber: number): string {
return `some text ${argNumber}`;
}
}
class DataValues {
f1: number = 64;
f2: string = 'Hello';
}
function example() {
const testClass = new ExampleOne();
const data = new DataValues();
for (const [key, value] of Object.entries(data)) {
const functionName = key as keyof ExampleOne;
const func = testClass[functionName];
const arg: number | string = value;
const result = func.apply(testClass, [arg]);
/* | <---------- error this*
|
|
The 'this' context of type
'((argStr: string) => string) | ((argNumber: number) => string)'
is not assignable to method's 'this' of type
'(this: ExampleOne, argStr: string) => string'.
Type '(argNumber: number) => string' is not assignable to type
'(this: ExampleOne, argStr: string) => string'.
Types of parameters 'argNumber' and 'argStr' are incompatible.
Type 'string' is not assignable to type 'number'.(2684) */
console.log(result);
}
}
example();
最初我试图这样做,但没有奏效:
for (const [key, value] of Object.entries(data)) {
const functionName = key as keyof ExampleOne;
const func = testClass[functionName];
const arg: number | string = value;
const result = testClass[functionName](arg);
/* | <--- error this
|
|
Argument of type 'string | number' is not assignable to parameter of type 'never'.
Type 'string' is not assignable to type 'never'.(2345)
*/
console.log(result);
}
解决方案
type FunctionCollection<V> = {
[K in keyof V]: (_: V[K]) => string;
}
function applyFunctions<V>(fc: FunctionCollection<V>, dv: V): void {
for (let key in dv) {
console.log(fc[key](dv[key]));
}
}
/////////////
// Example //
/////////////
class Values {
f1: string = 'Hello';
f2: number = 64;
}
class Functions {
f1(argStr: string): string {
return `some text ${argStr}`;
}
f2(argNumber: number): string {
return `some text ${argNumber}`;
}
}
function example() {
applyFunctions<Values>(new Functions(), new Values());
}
推荐阅读
- c++ - 我在 Linux VSCode (gcc) 和 Windows Clion (visual c++) 上的相同代码有不同的输出
- html - 响应方块的动态网格:
- ios - 如何在 url 加载时显示图像
- prolog - 谓词 nb_setarg/3 的意外结果
- reactjs - 如何根据我的链接地址添加字母?在反应
- excel - Excel:即使缺少一行,有没有办法从电源查询更新表格?
- excel - 每行复制 n 次
- javascript - useState 挂钩如何用于更新数组,即使它已分配但从未使用过?
- php - PHP,$_SESSION,iFrame,不在 iFrame 内保存会话,但在 iFrame 之外保存
- c - 在两个线程中安全地修改变量