question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Suggestion] Instruct users to return `z.NEVER` in `.superRefine()` when providing a type predicate

See original GitHub issue

As the validation result solely depends on whether ctx.addIssue() is called,

https://github.com/colinhacks/zod/blob/ad4385439d7c0d7a584052dee5aa578fde5a6efd/README.md?plain=1#L1891

and the return value is only used to satisfy the type predicate and is always thrown away,

https://github.com/colinhacks/zod/blob/c9e4ed4095d77f5b43a5ba11d8f76b56ea48e5c6/src/types.ts#L3821-L3822

to prevent users from accidentally forgetting to call ctx.addIssue() while returning a boolean value as described in the example,

https://github.com/colinhacks/zod/blob/ad4385439d7c0d7a584052dee5aa578fde5a6efd/README.md?plain=1#L1931-L1945

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:closed
  • Created 9 months ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
zetarakucommented, Dec 19, 2022

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 like refinement: (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 because z.NEVER is a value of never type, not a type itself.

0reactions
zetarakucommented, Dec 22, 2022

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. 🤔

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found