When it comes to the "Comparison" section, you can find information in the zod documentation referring to:
Runtypes
https://github.com/pelotom/runtypes
This library offers good support for type inference and includes read-only types functionality, which is not present in Zod.
Therefore, it's worth noting that Zod does not have support for read-only properties.
Nevertheless, the following code snippet demonstrates a working example:
import { z } from "zod";
const meUnknown: unknown = {
firstName: "Testy",
lastName: "mcTestface",
get myName() {
return `${this.firstName} ${this.lastName}`;
}
};
const MeSchema = z.object({
firstName: z.string(),
lastName: z.string(),
myName: z.string()
});
const me = MeSchema.parse(meUnknown);
/* The `me` variable has a type of:
{
firstName: string;
lastName: string;
myName: string;
}
*/
console.log(me.myName);
You may refer to this sandbox for further details.
However, it should be noted that Zod does not produce an error at the type level when assigning a new value like in the following example:
me.myName = 'something' // no type error here
This behavior is due to the fact that Zod lacks support for read-only types.