Hacker News new | past | comments | ask | show | jobs | submit login

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`.




You 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>;




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: