0

I am trying to declare and interface. Basically a user has a collection of roles. I am not able to understand what the right syntax in Typescript for this operation.

export interface User {
    firstName: string;
    lastName: string;
    roles: [Role]
}

export interface Role {
    accountId: string;
    roleId: string;
}
aolivera
  • 512
  • 1
  • 4
  • 23
  • `roles: Role[]` Have you tried, reading the docs on [Arrays](https://www.typescriptlang.org/docs/handbook/basic-types.html#array) ? – derpirscher Feb 12 '21 at 10:39

2 Answers2

0

It is:

export interface User {
    firstName: string;
    lastName: string;
    roles: Role[];
}
Rob Bailey
  • 920
  • 1
  • 8
  • 23
0

According to the TypeScript Handbook, you can make a collection of something like so:

export interface Role {
    accountId: string;
    roleId: string;
}

export interface User {
    firstName: string;
    lastName: string;
    roles: Array<Role> // Can also be written as Role[]
}

(Additionally, I think roles and accountId should be kept separate.)

Xavier
  • 423
  • 4
  • 11