用枚举属性将JSON对象映射到键入对象中

发布于 2025-02-11 12:35:07 字数 1418 浏览 1 评论 0原文

我想将我从API提取的JSON“字符串”转换为打字稿模型,如果我将其中一个道具从枚举更改为字符串,它很容易工作,但是我似乎无法将其映射到数小时搜索和尝试不同的事物。

我有一个模型,该模型拥有类似的类型

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[] = [];
}

,然后我从API中汲取类似的东西

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

,我只使用基本循环函数来映射每个元素并将其转换为类似的对象,

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,
      },
  );
}

但是我在哪里称呼我

convert(json)

我遇到了这个错误,此时我已经完全迷失了,我已经半个typescript了,我觉得我缺少一些简单的东西,任何帮助都会很大感谢。

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'.

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文