2 ms·
> and a whole new way to define types at runtime made them feel like it was too complicated How you made this happen? My impression was that TypeScript doesn't
by capableweb 5y ago
> and a whole new way to define types at runtime made them feel like it was too complicated
How you made this happen? My impression was that TypeScript doesn't have runtime types and stopped looking at TypeScript after that as it basically makes TypeScript useless for myself and my team.
- samhh 5y agoLibraries like io-ts and zod take care of this for you. For io-ts for example you just need to define a type guard function that asserts "x is Y" and away you go, you've got a runtime codec; it knows how to "decode" data, and it knows what that represents to the type system at compile-time. You'll generally only need to use the built-in combinators though e.g. `t.type({ prop: t.string })` produces a codec for `{ prop: string }`, and you can get that type at compile-time without effectively rewriting it using `t.TypeOf<typeof myCodec>`.
- mnahkies 5y agoIt's a nice library, although I personally prefer explicitly writing the type out beside the codec. Otherwise you have to translate the codec to the type in your head when reading the code without an ide that can show you the type. This also gives you a "test" if you will that the codec is parsing the structure you think it is