기본적으로 두 객체를 병합하는 일반 배열 축소 함수로 작업 할 입력을 얻으려고합니다. 다음 스 니펫은 실제 코드의 버려진 버전입니다. fl
의 유형이 {}
이고 IFoo & IBar
이 아닌 이유는 무엇입니까?배열이있는 TypeScript 교차 유형 reduce
(나는이 특정 예는 쉽게 하나의 Object.assign()
전화로 대체 될 수 있다는 것을 알고 있어요.)
const flatten = <K, T>(prev: K, x: T): K & T => {
return Object.assign(prev, x);
};
interface IFoo {
foo: true;
}
interface IBar {
bar: true;
}
const fooRes: IFoo = { foo: true };
const barRes: IBar = { bar: true };
const fl = [fooRes, barRes].reduce(flatten, {});
console.log(fl); // here, fl : {}
끔찍한 답변! 자세한 설명을 정말 감사드립니다. – marvinhagemeister