我为自定义对象形状创建一个类型,如下所示:
type A = {
readonly prop1: string;
readonly prop2: string;
}
然后在一种方法中我像这样使用它:
const getStuff = (obj: A) =>
obj.reduce((acc, curr) => {
...
})
}
这是在抱怨
property 'reduce' does not exist on type A
。我假设 A
作为对象类型会继承 JS 对象的所有本机方法,但我猜不是。
我怎样才能告诉它这样做?
因为原生 javascript 对象没有
reduce
方法。尝试 obj.toString()
或 obj.hasOwnProperty()
。也许您想Array.reduce
?如果是这样,请这样做:
type A<T> = {
readonly prop1: string;
readonly prop2: string;
} & Array<T>;