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
typeUserKeyBrand={readonlyUserKey: uniquesymbol}typeUserKey=string&UserKeyBrandconstUserKey=z.string().min(8).transform(k=>kasUserKey)typeProjectKeyBrand={readonlyProjectKey: uniquesymbol}typeProjectKey=string&ProjectKeyBrandconstProjectKey=z.string().min(8).transform(k=>kasProjectKey)typeUser=z.infer<typeofUser>// type User = {// _key: UserKey// }constUser=z.object({_key: UserKey})declareconstuk: UserKeyconstpk: ProjectKey=uk// Type 'UserKey' is not assignable to type 'ProjectKey'.// Property 'ProjectKey' is missing in type 'String & UserKeyBrand' but required in type 'ProjectKeyBrand'.ts(2322)
Using .brand:
declareconstUserKeyBrand: uniquesymboltypeUserKey=z.infer<typeofUserKey>constUserKey=z.string().min(8).brand<typeofUserKeyBrand>()declareconstProjectKeyBrand: uniquesymboltypeProjectKey=z.infer<typeofProjectKey>constProjectKey=z.string().min(8).brand<typeofProjectKeyBrand>()typeUser=z.infer<typeofUser>constUser=z.object({_key: UserKey})// type User = {// _key: string & z.BRAND<typeof UserKeyBrand>;// }declareconstuk: UserKeyconstpk: ProjectKey=uk// Type 'string & Zod.BRAND<typeof UserKeyBrand>' is not assignable to type 'string & Zod.BRAND<typeof ProjectKeyBrand>'.// Type 'string & BRAND<unique symbol>' is not assignable to type 'BRAND<unique symbol>'.// Types of property '[BRAND]' are incompatible.// Property '[ProjectKeyBrand]' is missing in type '{ [UserKeyBrand]: true; }' but required in type '{ [ProjectKeyBrand]: true; }'.ts(2322)
Now this is a simplified, distilled situation. Typically you have nested data where it gets really messy.
Also with type safety for the underlying type:
typeUserKeyBrand={readonlyUserKey: uniquesymbol}typeUserKey=string&UserKeyBrandconstUserKey=brand(z.string().min(8))<UserKey>()typeProjectKeyBrand={readonlyProjectKey: uniquesymbol}typeProjectKey=number&ProjectKeyBrand// notice the number, while we brand z.stringconstProjectKey=brand(z.string().min(8))<ProjectKey>()// error: Type 'ProjectKey' does not satisfy the constraint 'string'.ts(2344)functionbrand<O,Dextendsz.ZodTypeDef,I>(t: z.ZodType<O,D,I>){return<BextendsO>()=>t.transform(v=>vasB)}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Using
.brand
:Now this is a simplified, distilled situation. Typically you have nested data where it gets really messy.
Also with type safety for the underlying type:
Beta Was this translation helpful? Give feedback.
All reactions