[Suggestion] Instruct users to return `z.NEVER` in `.superRefine()` when providing a type predicate
See original GitHub issueAs the validation result solely depends on whether ctx.addIssue()
is called,
and the return value is only used to satisfy the type predicate and is always thrown away,
to prevent users from accidentally forgetting to call ctx.addIssue()
while returning a boolean value as described in the example,
instructing users to return z.NEVER
instead of a boolean at the end (indicating the value is never used) just like the early return case when providing a type predicate may be a good idea.
.superRefine((arg, ctx): arg is { first: string; second: number } => {
if (!arg) {
ctx.addIssue({
code: z.ZodIssueCode.custom, // customize your issue
message: "object should exist",
});
}
return z.NEVER; // this should satisfy the typing
})
Issue Analytics
- State:
- Created 9 months ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Typescript type predicate results in never
I'm suspecting the compiler is narrowing down to the common base type ClassX , however I'm not testing against the base class! Doesn't ......
Read more >zetaraku (Raku Zeta)
Learning is a never-ending journey. zetaraku has 40 repositories available. ... A utility site that provides a search interface for arcade games songs...
Read more >Colinhacks Zod Statistics & Issues - Codesti
Issue Title State Comments Created Date Updated Date
.catch() does not preserve type with .optional() open 0 2022‑12‑22 2022‑12‑22
URL validation broken on react native...
Read more >| notebook.community
If the filename extension is ``.gz`` or ``.bz2``, the file is first decompressed. Note that generators should return byte strings for Python 3k....
Read more >Viewing online file analysis results for 'JVC_40092.vbs'
Submit malware for free analysis with Falcon Sandbox and Hybrid Analysis technology. Hybrid Analysis develops and licenses analysis tools to fight malware.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi, @maxArturo.
I knew that the return type is necessary for the inferred refined type so I didn’t recommend users to declare their refinement function as
refinement: (arg: Output, ctx: RefinementCtx) => z.NEVER
but still likerefinement: (arg: Output, ctx: RefinementCtx) => arg is something
.I only encourage users to return a
z.NEVER
in the refinement function. (See the last snippet above)By the way,
refinement: (arg: Output, ctx: RefinementCtx) => z.NEVER
actually doesn’t work becausez.NEVER
is a value of never type, not a type itself.I made the PR above to update the docs. Please take a look at it and let me know if anything needs to be adjusted 😃 I wonder if the test cases should be updated to return
z.NEVER
too. 🤔