Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

120
Vistas
Mapping JSON object into Typed object with enum property

I am wanting to convert a JSON "string" that I pull from an api into my Typescript model, if i change one of the props from enum to string it works quite easily, however I cannot seem to map it to an enum despite hours of searching and trying different things.

I have a model that holds an array of types like this

export enum SEAT_TYPE {
  ECONOMY = "ECONOMY",
  PREMIUM_ECONOMY = "PREMIUM_ECONOMY",
  BUSINESS = "BUSINESS",
}

export type Seat = {
  number: string;
  type: SEAT_TYPE;
  status: boolean;
};

export class SeatingPlan {
  seating: Seat[] = [];
}

I then pull something like this from an api

  [{
    number: "1A",
    type: "BUSINESS",
    status: false,
  },
  {
    number: "1B",
    type: "BUSINESS",
    status: false,
  },
  {
    number: "1C",
    type: "BUSINESS",
    status: false,
  }]

An i'm using just a basic loop function to map over each element and convert it to a typed object like this

export function convert(json: Array<Seat>) {
  return json.map(
    (seat) =>
      <Seat>{
        number: seat.number,
        type: seat.type as keyof typeof SEAT_TYPE,
        status: seat.status,
      },
  );
}

But where i call my

convert(JSON)

I get this error and I'm totally lost at this point, i'm semi-new to typescript and i feel like i'm missing something quite simple, any help would be greatly appreciated.

Argument of type '{ number: string; type: string; status: boolean; }[]' is not assignable to parameter of type 'Seat[]'.
  Type '{ number: string; type: string; status: boolean; }' is not assignable to type 'Seat'.
    Types of property 'type' are incompatible.
      Type 'string' is not assignable to type 'SEAT_TYPE'.

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda