Is there any easy way to generate runtime type validators for compile-time type objects? So for example, say I've got something like this:
interface User {
id: string;
first: string;
last?: string;
age?: number;
}
I can write a user-defined type guard that does what I want:
function isUser(obj: any): obj is User {
return typeof obj == "object"
&& obj != null
&& typeof obj.first == "string"
&& (obj.last === undefined || typeof obj.last == "string")
&& (obj.age === undefined || typeof obj.age == "number")
}
However, with a lot of types and a lot of properties this becomes a lot to maintain, and even worse there has to be manual syncing between the defined type User and the type-guard assertions in isUser(). This begs for automation, generating the isUser() and other methods.