[Typescript] Zod in actions

import { z } from "zod";

export enum SUBTYPE {
  ABORT = "abort",
  START = "start",
  UPLOAD = "upload",
  LOADING = "loading",
}
export const TYPE = "print";
const PrintBase = z.object({
  type: z.literal(TYPE),
  subtype: z.nativeEnum(SUBTYPE),
});
type PrintBase = z.infer<typeof PrintBase>;

// start
export const PrintStart = PrintBase.merge(
  z.object({
    subtype: z.literal(SUBTYPE.START),
    attributes: z.object({
      tabId: z.number(),
    }),
  })
);
export type PrintStart = z.infer<typeof PrintStart>;

// upload
export const PrintUpload = PrintBase.merge(
  z.object({
    subtype: z.literal(SUBTYPE.UPLOAD),
    attributes: z.object({
      file: z.string(),
      tabId: z.number(),
      url: z.string().url(),
      uniqueFileId: z.string(),
    }),
  })
);
export type PrintUpload = z.infer<typeof PrintUpload>;

// abort
export const PrintAbort = PrintBase.merge(
  z.object({
    subtype: z.literal(SUBTYPE.ABORT),
  })
);
export type PrintAbort = z.infer<typeof PrintAbort>;

// loading
export const PrintLoading = PrintBase.merge(
  z.object({
    subtype: z.literal(SUBTYPE.LOADING),
  })
);
export type PrintLoading = z.infer<typeof PrintLoading>;

// Print
export const Print = z.discriminatedUnion("subtype", [
  PrintStart,
  PrintUpload,
  PrintLoading,
  PrintAbort,
]);
export type Print = z.infer<typeof Print>;

////////TESTING////////////

// #region test data
const printStart = {
  type: TYPE,
  subtype: SUBTYPE.START,
  attributes: {
    tabId: 123,
  },
} as const;

const printUpload: PrintUpload = {
  type: TYPE,
  subtype: SUBTYPE.UPLOAD,
  attributes: {
    file: "file",
    tabId: 123,
    url: "http://awefa.acom",
    uniqueFileId: "awefawe",
  },
};

const printLoading: PrintLoading = {
  type: TYPE,
  subtype: SUBTYPE.LOADING,
};

const printAbort: PrintAbort = {
  type: TYPE,
  subtype: SUBTYPE.ABORT,
};
// #endregion

function sendPrint<Sub extends SUBTYPE, Action extends Print>(
  subtype: Sub,
  obj: Action extends { subtype: Sub } ? Action : never
) {}

sendPrint(SUBTYPE.UPLOAD, printUpload);
Print.parse(printUpload);
sendPrint(SUBTYPE.START, printStart);
Print.parse(printStart);
sendPrint(SUBTYPE.LOADING, printLoading);
Print.parse(printLoading);
sendPrint(SUBTYPE.ABORT, printAbort);
Print.parse(printAbort);

 

posted @   Zhentiw  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-11-11 [Spring] Factory Pattern
2020-11-11 [Javascript] Broadcaster + Operator + Listener pattern -- 16. Map a Sequence Based on Values
2017-11-11 [ES6] Extends class in ES6 vs ES5 subclass
2016-11-11 [Node.js] Testing ES6 Promises in Node.js using Mocha and Chai
2016-11-11 [Angular2] @Ngrx/store and @Ngrx/effects learning note
2015-11-11 [Javascript] Intro to the Web Audio API
2014-11-11 [Firebase] 4. Firebase Object related Database
点击右上角即可分享
微信分享提示