Typescript 数组元素中的类型脚本推断
我有下一个代码:Typescript 数组元素中的类型脚本推断,typescript,typescript-typings,typescript-generics,Typescript,Typescript Typings,Typescript Generics,我有下一个代码: class A { constructor(public valueGetters: { getValue: <T>() => T }[]) {} getBundleData() { return this.valueGetters .map(v => v.getValue()) .reduce((acc, curr) => Object.assign(acc, curr)); } } const o
class A {
constructor(public valueGetters: { getValue: <T>() => T }[]) {}
getBundleData() {
return this.valueGetters
.map(v => v.getValue())
.reduce((acc, curr) => Object.assign(acc, curr));
}
}
const oneGetter = {
getValue: () => ({
hello: 'string',
}),
};
const otherGetter = {
getValue: () => ({
bye: 1,
}),
};
// the getters array
const getters = [oneGetter, otherGetter /*, ...more */]
// the array as parameters of the class
const ob = new A(getters);
// the code to merge the objects
const b = ob.getBundleData();
A类{
构造函数(公共估价师:{getValue:()=>T}[]){}
getBundleData(){
把这个还给我
.map(v=>v.getValue())
.reduce((acc,curr)=>Object.assign(acc,curr));
}
}
常数oneGetter={
getValue:()=>({
您好:'字符串',
}),
};
常数otherGetter={
getValue:()=>({
再见:1,,
}),
};
//getters数组
常量getter=[oneGetter,otherGetter/*,…more*/]
//数组作为类的参数
const ob=新的A(getter);
//合并对象的代码
常数b=ob.getBundleData();
我有一个数组中的dinamic getter。所有这些都使用getValue方法。我只是尝试将返回的值合并到一个唯一的对象(object.assign)中
Typescriopt可以以某种方式推断类型吗?不同参数的并集
这是迪那米克,然后我可以静态地倾斜它
我可以在类的
getBundleData()
方法中实现getter的并集吗?类必须是泛型的。