Germán
Germán

Reputation: 1321

JS - How to define multi status property on object

I have this interface created on Angular in order to "shape" the object between the front and the server.

export interface RepairNote{
  id: number,
  nombre?: string,
  telefono?: number,
  ref?: string
  marca?: string,
  averia?: string,
  fecha?: Date,
  estado?: /* here */
}

I'm willing to have "estado" (English: status) with a different closed group of status, being "Pending, Complete and Closed". Meaning that estado can only have one of these 3 values. In my mind it works like a boolean, but with more than 2 status. This is a very simple question, I know it belongs to the basics of OOP, but I got blank and I don't know how to make it work.

Upvotes: 0

Views: 139

Answers (1)

Tomasz Mikus
Tomasz Mikus

Reputation: 1296

I usually use "enum" for these sort of things:

export enum Status {
  Pending = 'Pending',
  Complete = 'Complete',
  Closed = 'Closed',
}

export interface RepairNote {
  // ...
  status?: Status;
  // ...
}

Here you can find more information about enums: https://www.typescriptlang.org/docs/handbook/enums.html

Upvotes: 1

Related Questions