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.

Removing the type guards?

See original GitHub issue

Saw the note about removing check, but if it is simply safeParse().success, why can we not implement it that way in the base type? I assume this has already been tried, but if there is a problem with that, it would be helpful to know what the problem is so that I don’t accidentally reproduce the issue in my own usage.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
scotttrinhcommented, Jan 15, 2021

Wrote this little wrapper that seems to do the right thing:

function check<T>(schema: ZodSchema<T>, data: unknown): data is T {
  const parsed = schema.safeParse(data);
  return parsed.success;
}

— But still not clear why this can’t be how ZodType#check is implemented to avoid having to import this helper when using a ZodSchema

0reactions
ChristoRibeirocommented, May 19, 2022

Finally I used the same way as @scotttrinh did. It works nicely with next-rest.

Full context here: https://github.com/joeltg/next-rest/issues/1

// zod-check.ts
import { ZodSchema } from "zod"

export function check<T>(schema: ZodSchema<T>) {
  function is(data: unknown): data is T {
    return schema.safeParse(data).success
  }
  return { is }
}
// pages/api/widgets/[id].ts
...
import { ZodSchema } from "zod"
import { check } from "./zod-check"

const getRequestHeaders = z.object({ accept: z.literal("application/json") })
const getRequestBody = z.void()

export default makeHandler("/api/widgets/[id]", {
  GET: {
    headers: check(getRequestHeaders).is,
    body: check(getRequestBody).is,
    exec: async ({ params }) => {
      // ... method implementation
      return {
        headers: { "content-type": "application/json" },
        body: { id: params.id, isGizmo: false },
      }
    },
  },
})

Wrote this little wrapper that seems to do the right thing:

function check<T>(schema: ZodSchema<T>, data: unknown): data is T {
  const parsed = schema.safeParse(data);
  return parsed.success;
}

— But still not clear why this can’t be how ZodType#check is implemented to avoid having to import this helper when using a ZodSchema

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript: How Type Guards Can Help You Get Rid of 'as'
In this article, I will go into more detail about “as” and explain how to avoid it by using type guards, which represent...
Read more >
Documentation - Narrowing - TypeScript
It looks at these special checks (called type guards) and assignments, ... and TypeScript still correctly removes null from the type of strs...
Read more >
Type guard removing string literal types - Stack Overflow
When creating a TS type guard, it seems like string literals are removed from the narrowed type once you add undefined or null...
Read more >
How To Do Anything in TypeScript With Type Guards
Remove all values null or undefined values from array. Using the isDefined type guard we just defined, we can use it with the...
Read more >
Keeping TypeScript Type Guards safe and up to date (a ...
Type guards allow us to check that a runtime, unknown object (think ... is not shared with other scopes—is removed at compile time...
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