I am currently working with TypeScript version ^4.1.3 and have developed a REST API that deals with albums and art collections. Before sending the response to the web client, I make sure to remove the userId property from the collections.
Below are my Album and Arts interfaces from DynamoDB tables, along with a utility method to remove the userId property from collection objects.
export interface Album {
userId: string;
albumId: string;
creationDate: string;
visibility: AlbumVisibility;
title: string;
description: string;
coverUrl: string;
}
export interface Art {
albumId: string;
artId: string;
sequenceNum: number;
userId: string;
creationDate: string;
title: string;
description: string;
imgUrl: string;
}
export function rmUserIdFromArr(items: Album[] | Art[]) {
return items.map((item: Album | Art) => {
const { userId, ...newItem } = item;
return newItem;
});
}
However, when running the code on a collection of art items, TypeScript is throwing an error:
const arts = rmUserIdFromArr(result.items).map((item) => ({
...item,
imgUrl: getDownloadSignedUrl(getFsArtsFolder(item.albumId), item.artId),
}));
The error message says: 'Property 'artId' does not exist on type '{ albumId: string; creationDate: string; visibility: >AlbumVisibility; title: string; description: string; coverUrl: string; } | { albumId: string; >artId: string; sequenceNum: number; creationDate: string; title: string; description: string; >imgUrl: string; }'. Property 'artId' does not exist on type '{ albumId: string; creationDate: string; visibility: >AlbumVisibility; title: string; description: string; coverUrl: string; }'.ts(2339)
This seems odd to me as the artId property is clearly defined in the Art interface type.
Am I overlooking something? While I can resolve this by setting the item within the map function to item: any, I was hoping for a better solution.
const arts = rmUserIdFromArr(result.items).map((item: any) => ({
Thank you very much, BR.