0

I'm using typescript version ^4.1.3 and have a REST API with albums and arts collections. Before I send the request response to web client, I remove the userId from collections.

Please see my Album and Arts interfaces from DynamoDB tables, and the 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;
    });
}

But when I run the code in a collection of arts items, Typescript is complaining of:

const arts = rmUserIdFromArr(result.items).map((item) => ({
        ...item,
        imgUrl: getDownloadSignedUrl(getFsArtsFolder(item.albumId), item.artId),
    }));

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 is strange to me, because the artId exists in the Art interface type.

Am I missing something here? I know I can fix the issue setting the item from map function as item: any, but I was wondering if there`s a better way to fix this.

const arts = rmUserIdFromArr(result.items).map((item: any) => ({

Thank you very much, BR.

2
  • 2
    Please consider modifying the code in this question so as to constitute a minimal reproducible example which, when dropped into a standalone IDE like The TypeScript Playground, clearly demonstrates the issue you are facing (so no undeclared types or values; the only error present should be the one you are asking about). This will allow those who want to help you to immediately get to work solving the problem without first needing to re-create it. And it will make it so that any answer you get is testable against a well-defined use case. Commented Jun 8, 2021 at 19:58
  • 1
    What should happen if inside map iteration item has type Album? Commented Jun 8, 2021 at 20:21

1 Answer 1

2

I believe you should rewrite your function to something more generic:

function rmUserIdFromArr<T extends { userId: string }>(items: T[]): Omit<T, 'userId'>[] {
    return items.map((item) => {
        const { userId, ...newItem } = item;
        return newItem;
    });
}

playground link

Sign up to request clarification or add additional context in comments.

2 Comments

Nice solution!!
Thank you very much @aleksxor. Very nice solution and it worked!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.