3 ms·
Not related to the new assert signatures feature (which is great!), but IMO the two best approaches to do what you want today are: - io-ts[1] This requires yo
by frankpf 7y ago
Not related to the new assert signatures feature (which is great!), but IMO the two best approaches to do what you want today are:
- io-ts[1]
This requires you to write your types as a runtime value, and allows you to extract static types from those, e.g.:
const ContactInfo = t.type({
address_1: t.string,
...
})
type ContactInfo = t.TypeOf<typeof ContactInfo>
You can validate objects with `ContactInfo.decode(someObject)`
Note that we can have the same name for the type and value because they live in different namespaces. When you do ContactInfo.decode, you're calling the decode property of `const ContactInfo`. When you use `ContactInfo` in a type position (e.g. `function x(arg: ContactInfo)`), you're using the `type ContactInfo` declaration
- typescript-is[2]
This uses TypeScript's transformer API. You can use it with https://github.com/cevek/ttypescript https://github.com/cevek/ttypescript. It generates validators for your interfaces/types/etc at compile-time.
[1]: https://github.com/gcanti/io-ts https://github.com/gcanti/io-ts
[2]: https://github.com/woutervh-/typescript-is https://github.com/woutervh-/typescript-is
- joshAg 7y agotypescript-is looks like it can be a drop-in solution since we're only dealing with PODS. Thanks!