1

I'm trying to convert an object of values coming from Firebase to a typed array of values:

const snapshot = await db.teams().once('value');
const teams: Array<ITeam> = Object.entries(snapshot.val()).map(
    ([id, { identifier, name }]): ITeam => {
        return { identifier, name, id };
    }
);

Types are modelled like following:

export interface ITeam extends ITeamEntry {
    id: string;
}

export interface ITeamEntry {
    identifier: string;
    name: string;
}

But I'm getting errors like:

Property 'identifier' does not exist on type 'unknown'.
Property 'name' does not exist on type 'unknown'.

I'm not sure how to fix this.

1
  • Shouldn't ILeague be a type attribute of ITeam ? Commented Jul 18, 2019 at 12:12

1 Answer 1

3

val is probably an object with unknown properties. To be able to access the properties you will probably need to assert val to Record<string, ITeam>

const teams: Array<ITeam> = Object.entries(snapshot.val() as Record<string, ITeam>).map(
    ([id, { identifier, name }]): ITeam => {
        return { identifier, name, id };
    }
);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I was missing the Record type. I was not aware of it.

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.