Runtime Introspection of Flow Types in JS(medium.com) |
Runtime Introspection of Flow Types in JS(medium.com) |
type User = {name: string, isAdmin: boolean};
// compiles to
const User = t.object(
t.property('name', t.string()),
t.property('isAdmin', t.boolean())
);
// which you can then use with
User.check({name: 'Alice'}); // boolean
User.assert({nom: false}); // throws
Ultimately it should be possible to use this to implement pattern matching in JS based on flow types, for example: type User = {
name: string;
roles: Role[];
};
type Role = {
id: number;
name: string;
users: User[]
};
const url = match(someInput,
(user: User) => `/users/${user.name}`,
(role: Role) => `/roles/${role.id}`,
(_) => `/` // default URL
);
The really hard thing is to do this whilst maintaining perfect compatibility with flow.