3 ms·
If you're receiving JSON data, you can not validate the data at compile time. But yes: I think TS is missing a way to derive runtime functionality from the typ
by Garlef 6y ago
If you're receiving JSON data, you can not validate the data at compile time.
But yes: I think TS is missing a way to derive runtime functionality from the type definitions - at least in the cases, where the types describe plain JSON.
Something along the lines of clojures' `spec`.
- jakelazaroff 6y agoYou can do the converse: create a type guard asserting a structure, and then extract a type. type Spec<T> = T extends (x: unknown) => x is infer U ? U : never; type Foo = Spec<typeof someTypeGuard>;