0

Here is what I have tried:

class PhraseService

    phrasePosShortNames: [{ id: number, name: string }];
    phrasePosNames = [
    {
        id: 0,
        longName: '#',
        shortName: '#'
    },
    {
        id: 1,
        longName: 'Noun',
        shortName: '#'
    },
    {
        id: 2,
        longName: 'Verb',
        shortName: 'V'
    }
    ];
    constructor( ) {

        this.phrasePosShortNames = this.phrasePosNames.map(item => {
            id: item.id,
            name: item.longName
        })
    }

But this gives me an error:

Severity Code Description Project File Line Suppression State Error TS2322 Type 'void[]' is not assignable to type '[{ id: number; name: string; }]'. Property '0' is missing in type 'void[]'

Can someone tell me what I'm doing wrong?

1 Answer 1

3
this.phrasePosNames.map(item => {
    return {
      id: item.id,
      name: item.longName
    };
});

And the type should be

{ id: number, name: string }[]

or

Array<{ id: number, name: string }>

I would define an interface instead, which would make the code more readable.

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

2 Comments

Thanks. My mistake there but I still get a typescript error: error TS2322: Build: Type '{ id: number; name: string; }[]' is not assignable to type '[{ id: number; name: string; }]'.
Fixed with my definition by adding [] to the end of the { ... }

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.