首页 > 其他分享 >[Typescript] Zod in actions

[Typescript] Zod in actions

时间:2022-11-11 20:57:17浏览次数:40  
标签:Typescript Zod subtype actions SUBTYPE export Print const type

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

 

标签:Typescript,Zod,subtype,actions,SUBTYPE,export,Print,const,type
From: https://www.cnblogs.com/Answer1215/p/16881831.html

相关文章

  • typescript装饰器
    属性装饰器参数exportdefaultfunction(proto,key){//两个参数}给属性增加metadataimport'reflect-metadata';exportdefaultfunction(label,type?......
  • [Typescript] 97. Hard - Capitalize Words
    Implement CapitalizeWords<T> whichconvertsthefirstletterof eachwordofastring touppercaseandleavestherestas-is.Forexampletypecapitalized......
  • 安装 TypeScript 并编译成JS
    官网:https://github.com/microsoft/TypeScriptTypeScript是一种由微软开发的开源、跨平台的编程语言。它是JavaScript的超集,最终会被编译为JavaScript代码。TypeScript......
  • OJ中Typescript语法整理
    基础原始类型原始类型:number/string/boolean/null/undefined/symbol对象类型:oject(数组,对象,函数等)自定义复杂的对象类型:typeCustomArray=(number|string)let......
  • [Typescript] 95. Hard - Required Keys
    Implementtheadvancedutiltype RequiredKeys<T>,whichpicksalltherequiredkeysintoaunion.ForexampletypeResult=RequiredKeys<{foo:number;bar?:......
  • [Typescript] 96. Hard - Optional Keys
    Implementtheadvancedutiltype OptionalKeys<T>,whichpicksalltheoptionalkeysintoaunion. /*_____________YourCodeHere_____________*/typeOp......
  • typescript 泛型
    一、泛型与any类型的区别泛型是等待确定的占位类型,可以理解为函数的形参;所以泛型是固定的某一个类型,实例化的时候确定其实际类型any类型是顶级类型,它包括了所有的基......
  • [Typescript] 93. Hard - Get Required
    Implementtheadvancedutiltype GetRequired<T>,whichremainsalltherequiredfieldsForexampletypeI=GetRequired<{foo:number,bar?:string}>//exp......
  • [Typescript] 94. Hard - Get Optional
    Implementtheadvancedutiltype GetOptional<T>,whichremainsalltheoptionalfieldsForexampletypeI=GetOptional<{foo:number,bar?:string}>//expe......
  • TypeScript(基础篇)day01
    一.TS介绍1.1简介ts是2012年由微软开发,在js的基础上添加了类型支持1.2优劣势优势:任何位置都有代码提示,增加效率;类型系统重构更容易;使用最新的ECMAscript语法劣势:和......