Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda