You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to create a zod instance to describe an object, whose keys should not be "$and" and the value should be a string or a number. This is the simplified code I wrote, It works fine in type check, but the success is false.
constZRecord=z.record(z.string(),z.union([z.string(),z.number()])).and(z.record(z.enum(["$and"]),z.never()));typeIRecord=z.infer<typeofZRecord>;constzinvalid: IRecord={$and: 1};// Type 'number' is not assignable to type 'undefined'.ts(2322). this is want I wantconstzvalid: IRecord={a: 1};console.log(ZRecord.safeParse(zinvalid));// {success:false}console.log(ZRecord.safeParse(zvalid));// {success:false}typeTRecord={[key: string]: number|string}&{[keyin"$and"]?: never;};constinvalid: TRecord={$and: 1};// Type 'number' is not assignable to type 'undefined'.ts(2322). this is want I wantconstvalid: TRecord={a: 1};
The final goal is to create a zod instance for valid data like this
typeTFilter=|({[key: string]: string|number}&{[keyin"$and"|"$or"]?: never;})|({$and: string[]}|{$or: number[]});consttvalid1: TFilter={a: 1};// normal keys is string or numberconsttvalid2: TFilter={$and: ["key"]};// the value of special key '$and' is a string array and this is the only key allowed hereconsttvalid3: TFilter={$or: [1]};// the value of special key '$or' is number array and this is the only key allowed here
The text was updated successfully, but these errors were encountered:
I want to create a zod instance to describe an object, whose keys should not be "$and" and the value should be a string or a number. This is the simplified code I wrote, It works fine in type check, but the success is false.
The final goal is to create a zod instance for valid data like this
The text was updated successfully, but these errors were encountered: