Point-free `.parse` not possible since v3.9.0 – a feature or a bug?
See original GitHub issueHi!
Zod is an awesome project. And for a while I had code looking like this when parsing data from API endpoints:
import { z } from "zod";
type User = z.infer<typeof User>;
const User = z.object({
name: z.string()
});
function getUser(): Promise<User> {
return getUserFromApi().then(User.parse);
}
getUser()
.then(
// Use User
)
.catch(
// Handle parsing error
);
This is just a simplified example, but the idea is that User.parse
is called by .then
to parse whatever data is coming from the Promise. If User.parse
throws an error, the whole thing results in a rejected Promise. Nice and simple and without the need to write .then(user => User.parse(user))
.
However, since the release of v3.9.0, .parse
cannot be used in a point-free way. An error is thrown inside Zod:
TypeError: Cannot read properties of undefined (reading 'safeParse')
Now, I want to ask if this behaviour is intentional or if it’s a bug?
Disclaimers:
- I know that many people discourage point-free programming in JavaScript. It can be problematic, as the error above demonstrates, but I still happen to use the technique when I find that it simplifies my code.
- It’s absolutely fine with me if this is not considered a bug. It’s a small thing, and I’ll keep using Zod even if I can’t use
.parse
in a point-free way. I just wanted to make sure that I reported my findings.
I created a CodeSandbox that shows running code in v3.8.2, but if you change the version to v3.9.0 under “Dependencies”, the example breaks: https://codesandbox.io/s/point-free-zod-parse-473q1?file=/src/index.ts
Finally, thank you for Zod. It’s truly awesome 🙏
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (1 by maintainers)
Top GitHub Comments
Bug! Fixed in 3.12.1 - sorry about that.
Thanks I’ll keep this open