One of your mistakes is that you want the return of the reduce
to be equals to string[]
, but at the same time you are trying to use a
as a.stringArray
.
Look at the following playground.
type ObjectDefinition = {
stringArray: string[];
};
type ArrayOfObject = ObjectDefinition[];
const arrayOfObjects: ArrayOfObject = [{
stringArray: ['foo'],
}, {
stringArray: ['bar', 'dog', 'cat'],
}];
const testArray: string[] = arrayOfObjects.reduce((a: string[], b: ObjectDefinition) => {
return a.concat(b.stringArray);
}, []);
console.log(testArray);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…