Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Don't we mostly see this from json responses? The example they gave adds overhead, checking each product form an array. We use some responses with 200 products, we don't want this extra latency.

Does JS need a better way to introduce json type safety? C# and graphql use schemas, I wonder if this is a better approach (compile time, not run time).



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: