Как правильно выполнить типизацию (TypeScript) функции трансформации данных?
Всем привет, у меня есть функция трансформации данных:
const orders = [ { "date": "2017-10-16 12:07:07", "docTypesName": "Приход", "docId": 564564867361367, "image": "https://www.komus.ru/medias/sys_master/root/hd3/h93/9286922043422.jpg", "name": "Молочный Изюм 100", "price": 102, "quantity": 45, "removed": 0 }, { "date": "2017-10-16 12:07:07", "docTypesName": "Приход", "docId": 564564867361367, "image": "https://mariupolcena.com/files/products/9ff44136e6ccb0afb404ad26f727e67d.jpeg", "name": "Русская картошка чедар 50", "price": 46.3, "quantity": 45, "removed": 0 }, { "date": "2017-10-16 12:07:07", "docTypesName": "Расход", "docId": 564564867361367, "image": "https://mariupolcena.com/files/products/9ff44136e6ccb0afb404ad26f727e67d.jpeg", "name": "Русская картошка чедар 50", "price": 46.3, "quantity": 45, "removed": 0 }, { "date": "2017-11-29 17:26:57", "docTypesName": "Расход", "docId": 564564867361365, "image": "https://www.komus.ru/medias/sys_master/root/hd3/h93/9286922043422.jpg", "name": "Молочный Изюм 100", "price": 102, "quantity": 6, "removed": 0 }, ] interface IOrder { date: string, docTypesName: string, docId: number, image: string, name: string, price: number, quantity: number, removed: number, } interface IProduct { image: string, name: string, price: number, quantity: number, } interface IDocument { date: string, docId: number, docTypesName: string, products: IProduct[], } interface IElement { date: string, documents: IDocument[] } interface IResult { '2017-10-16': IElement, } function f(orders: IOrder[]) { const result: IElement = orders.reduce((accumulator: any, currentValue: IOrder) => { const date = currentValue.date.split(' ')[0]; if (!accumulator[date]) { accumulator[date] = { date, documents: {}, } } if (!accumulator[date].documents[currentValue.docTypesName]) { accumulator[date].documents[currentValue.docTypesName] = { date: currentValue.date, docId: currentValue.docId, docTypesName: currentValue.docTypesName, products: [], } } accumulator[date].documents[currentValue.docTypesName].products.push({ name: currentValue.name, price: currentValue.price, image: currentValue.image, qunatity: currentValue.quantity, }) return accumulator; }, {}) console.log('result', result); return Object.values(result).map((currentValue: IElement) => { console.log('currentValue', currentValue); if (currentValue.documents) { currentValue.documents = Object.values(currentValue.documents); } return currentValue; }); } console.log(f(orders)); Подскажите пожалуйста, как более правильно описать типизацию и как правильно типизировать вот этот момент, соответственно избавиться от any? const result: IElement = orders.reduce((accumulator: any, currentValue: IOrder) => { |
Как-то так:
interface IOrder { date: string, docTypesName: string, docId: number, image: string, name: string, price: number, quantity: number, removed: number, } interface IProduct { image: string, name: string, price: number, quantity: number, } interface IDocument { date: string, docId: number, docTypesName: string, products: IProduct[], } interface IElement { date: string, documents: IDocument[] } interface IElementMap { date: string, documents: Record<string, IDocument> } type IResultMap = Record<string, IElementMap>; function f(orders: IOrder[]): IElement[] { const result = orders.reduce((accumulator, currentValue) => { const date = currentValue.date.split(' ')[0]; if (!accumulator[date]) { accumulator[date] = { date, documents: {}, } } if (!accumulator[date].documents[currentValue.docTypesName]) { accumulator[date].documents[currentValue.docTypesName] = { date: currentValue.date, docId: currentValue.docId, docTypesName: currentValue.docTypesName, products: [], } } accumulator[date].documents[currentValue.docTypesName].products.push({ name: currentValue.name, price: currentValue.price, image: currentValue.image, quantity: currentValue.quantity, }) return accumulator; }, {} as IResultMap) console.log('result', result); return Object.values(result).map(currentValue => { console.log('currentValue', currentValue); return { ...currentValue, documents: Object.values(currentValue.documents) } }); } console.log(f(orders)); |
Спасибо за помощь.
Наверное ещё вот так можно: const result = orders.reduce<IResultMap>((accumulator, currentValue) => { |
Часовой пояс GMT +3, время: 15:04. |