Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type assertions #2

Closed
m-rutter opened this issue Mar 12, 2020 · 4 comments
Closed

Type assertions #2

m-rutter opened this issue Mar 12, 2020 · 4 comments

Comments

@m-rutter
Copy link

m-rutter commented Mar 12, 2020

I've been thinking a lot about validation libaries and I share your frustration with Yup. Yup does not get everything right. I'm scared about to introducing io-ts is it has a high a cognitive overhead, but I really love what io-ts does.

I think I'll experiment with this libarary a bit, but in terms of io-ts features do you think zod could be improve with these features?

  • built in type guards - i.e. io-ts's is() method on codecs.
  • built in type assertions - (maybe call it assert()) using the new assertion function types in typescript 3.7 (io-ts doesn't actually have this probably because it wants us to use Either instead of exceptions)
  • branded types - doing io-ts's symbol trickery to create types like NonZeroNumber or Email
    • it might be difficult to do that one while remaining ergonomic
@colinhacks
Copy link
Owner

colinhacks commented Mar 13, 2020

Just implemented type guards and published it to zod 1.0.5. I'm using io-ts's same .is() API. That was an easy one :)

Tried to implement type assertions as well but it looks like it's not possible to do that yet without requiring duplicative typings :/ We'll have to wait and see on that.

I'm splitting off the feature request for branded types into a different issue 👍

@colinhacks colinhacks changed the title missing io-ts's features - assertion and branded types Type assertions Mar 13, 2020
@colinhacks colinhacks mentioned this issue Mar 13, 2020
@colinhacks
Copy link
Owner

For future reference, the .is() method has been replaced by .check(). .is() will still work until the release of zod@2 but it should be considered deprecated.

I'm closing this, since type assertions don't appear to work currently for generic classes such as ZodType. If this changes in the future I'll look into it again. And of course if someone cleverer than me figures out how to make assertions work, PRs are welcome!

@vlinder
Copy link

vlinder commented Oct 19, 2022

Whatever happened to .is/.check? Can we get them back? Is there another way of doing this now that I'm missing?

I'm using 3.19.1 and can't seem to find anything in the docs..

@ceefour
Copy link

ceefour commented Dec 26, 2022

https://www.npmjs.com/package/typed-assert mentions:

Consider using zod
While this library does a fine job for most simple use cases, please consider using zod if you need more complex assertions.

It doesn't seem that zod is able to perform TypeScript 3.7 assertions the way typed-assert does it.
I've tried using .parse() and the checked value is still "not asserted (at compile-time)".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants