-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Exporting schemas from package breaks instanceof
#2241
Comments
I am also having this same issue! Any updates? |
Hey 👋
Make |
Thanks, it solved the issue. However, if I set the I also had to remove the Also, another question. Are these kinds of problems less common in pnpm? |
I ran into this problem with one of our internal libraries. It's because it checks the prototype and the instances from different copies of the library have different prototypes. It's the reason we have I solved it by overriding the https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof |
Same problem here. Tried setting it as a peer dependency in the consumed package and made sure that the workspace only has one |
So yea because of version differences between the packages. Not sure if there is a good way to handle this besides having the same version. import { mySchema } from 'mylib';
import { z } from 'zod';
try {
mySchema.parse('');
} catch (error) {
console.log(error instanceof mySchema.ZodError); // true
} |
Temporarily did |
This can also happen even with a single zod installation, without iframes or anything, if one part of the code accesses the commonjs export ( Issue in trpc-cli: mmkal/trpc-cli#7 If anyone's interested, a non-bullet-proof-but-in-most-cases-good-enough fix is to look at the constructor name as above, but you should also account for sub-classes: export const looksLikeInstanceof = <T>(value: unknown, target: new (...args: any[]) => T): value is T => {
let current = value?.constructor
do {
if (current?.name === target.name) return true
current = Object.getPrototypeOf(current) as Function
} while (current?.name)
return false
} PR in trpc-cli: mmkal/trpc-cli#8 CC @colinhacks in case you know of some reason why the method above wouldn't be a good idea for some reason. Given zod ships both commonjs and esm, so has the dual-package hazard, it would be nice if it used Also related: evanw/esbuild#3333 |
Oh wow. Embarrassingly I wasn't aware of But of course there are always scenarios where multiple versions of Zod will be floating around. In Zod 4 all schemas will have a |
Yeah, it took me a bit of searching for a solution before I found it. I also ran into it with one of our internal packages, which is why I had to find a solution. 😃 I had difficulty finding example implementations for my use case, but I store an object at a key in each instance of my class and in |
Related. Hard lesson learned here 😂 |
Hi @colinhacks: we're running up against this issue. Would you accept a contribution with |
Note to self (and hopefully others): |
When I export a schema from a package and use that schema in another package, the
instanceof
operator doesn't work onZodError
orSchema
. I'm also new to creating packages, so maybe there is something that I miss there!How I'm using the exported schemas:
I have also created a demo project.
I call the package that I create schemas in
mylib
and the package that I'm using those, is calledmyprogram
. I'm usingnpm link
to addmylib
tomyprogram
package. And also, I'm transpiling themylib
withnpx tsc
. Then you can test the code inmyprogram
withnpx tsx index.ts
.A maybe related thing that I read about this. But I also checked that you are addressing this issue on
ZodError
class constructor.The text was updated successfully, but these errors were encountered: