如何将收到的字符串转换为枚举

发布于 2025-02-06 05:57:26 字数 727 浏览 1 评论 0 原文

我正在从文件中接收一些JSON对象,我想将一些字段解析为预定义的值。

import jobsRaw from '../../data/jobs.json';

我创建了一种描述我对象的类型,但是我希望 status 只有我的预定义值,如果我可以定义后返回值。

export type Job = {
  type: string;
  id: string;
  status: JobStatus;
  warehouseId: string;
  relatedCell: string;
  relatedDocument: string;
  partnerName: string;
  potatoType: string;
  relatedBoxes: string[];
  boxCount?: number;
  createdAt: string;
  completedAt: string;
};

这是我尝试使用的映射:

const jobs: Job[] = jobsRaw.map((job: Job) => ({
  ...job,
  status: JobStatus[job.status],
  boxCount: job.relatedBoxes.length,
}));

我想创建某种JobStatus类型,我可以用它来解析我收到的字符串值。

I am receiving some json object from a file, and I want to parse some of the fields to predefined values.

import jobsRaw from '../../data/jobs.json';

I created a type that describes my object, but I want the status to have only my predefined values, if else I might define a fallback value.

export type Job = {
  type: string;
  id: string;
  status: JobStatus;
  warehouseId: string;
  relatedCell: string;
  relatedDocument: string;
  partnerName: string;
  potatoType: string;
  relatedBoxes: string[];
  boxCount?: number;
  createdAt: string;
  completedAt: string;
};

This is the mapping I'm trying with:

const jobs: Job[] = jobsRaw.map((job: Job) => ({
  ...job,
  status: JobStatus[job.status],
  boxCount: job.relatedBoxes.length,
}));

I want to create some sort of JobStatus type that I can parse my received string value with.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法言说的痛 2025-02-13 05:57:26

如果我必须实施此功能,我会选择检查数据是否是正确的 JobStatus 。也许类似的事情:

const isJobStatus = (value: any): value is JobStatus =>
  value && Object.values(JobStatus).includes(value);

const jobs: Job[] = jobsRaw.map((job: Job) => ({
  ...job,
  status: isJobStatus(job.status) ? job.status : JobStatus.SOME_DEFAULT_STATUS,
  boxCount: job.relatedBoxes.length,
});

使用这种方法,您甚至可以在下面使用Map Iterator中使用其他类型的类型来更好地表示“原始”数据,因为类型的谓词会将 job.status 作为适当的 JobStatus 当它返回true时。我还在 job 中进行了 boxcount 强制性,并从 jobRaw 中将其删除,您不需要使其可选。

export type Job = {
  type: string;
  id: string;
  status: JobStatus;
  warehouseId: string;
  relatedCell: string;
  relatedDocument: string;
  partnerName: string;
  potatoType: string;
  relatedBoxes: string[];
  boxCount: number;
  createdAt: string;
  completedAt: string;
};

type JobRaw = Omit<Job, 'status' | 'boxCount'> & { status: string };

const jobs: Job[] = jobsRaw.map((job: JobRaw) => ({
  ...job,
  status: isJobStatus(job.status) ? job.status : JobStatus.SOME_DEFAULT_STATUS,
  boxCount: job.relatedBoxes.length,
}));

If I had to implement this I would go for a type predicate to check if the data is a proper JobStatus. Maybe something like this:

const isJobStatus = (value: any): value is JobStatus =>
  value && Object.values(JobStatus).includes(value);

const jobs: Job[] = jobsRaw.map((job: Job) => ({
  ...job,
  status: isJobStatus(job.status) ? job.status : JobStatus.SOME_DEFAULT_STATUS,
  boxCount: job.relatedBoxes.length,
});

With this approach you could even use a different type in map iterator like below to better represent the "raw" data because the type predicate will cast job.status as a proper JobStatus when it returns true. I've also made boxCount mandatory in Job and removed it from JobRaw, by doing like this you won't need to make it optional.

export type Job = {
  type: string;
  id: string;
  status: JobStatus;
  warehouseId: string;
  relatedCell: string;
  relatedDocument: string;
  partnerName: string;
  potatoType: string;
  relatedBoxes: string[];
  boxCount: number;
  createdAt: string;
  completedAt: string;
};

type JobRaw = Omit<Job, 'status' | 'boxCount'> & { status: string };

const jobs: Job[] = jobsRaw.map((job: JobRaw) => ({
  ...job,
  status: isJobStatus(job.status) ? job.status : JobStatus.SOME_DEFAULT_STATUS,
  boxCount: job.relatedBoxes.length,
}));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文