What does HackerNews think of json-schema-to-typescript?

Compile JSONSchema to TypeScript type declarations

Language: TypeScript

#27 in TypeScript
Nice! Lots of tricky edge cases to do this right: there’s ambiguity in the JSON Schema spec, version-to-version changes, many popular community conventions that don’t adhere to the spec, etc. Feel free to check out the tests to understand some of these: https://github.com/bcherny/json-schema-to-typescript (shameless plug — I have maintained this library for a number of years).
jsonnet is my go-to language for anything related to configuration, after having tried json, yaml, TS, edn, and tasting dhall and toml. It addresses all problems in the article and more.

the composition model strikes a good balance between data extensibility / language expressiveness / ease of use.

the generated json leads to easy-to-understand and portable data, and if you write jsonschemas from jsonnet, tools like json-schema-to-typescript [1] make it easy to import a consistent interface, and almost every language has a reasonably up-to-date validation library.

[1] https://github.com/bcherny/json-schema-to-typescript

Take a look at https://github.com/bcherny/json-schema-to-typescript, too. I used it successfully at a previous job. IIRC, I had to write some code to convert OpenAPI to JSON Schema but it wasn’t onerous
If you're using OpenAPI, you could use this to generate TypeScript interfaces:

https://github.com/bcherny/json-schema-to-typescript

It works really well

Should be technically possible, though I don't know how strictly they map to/from each other.

JSON schema to TypeScript - https://github.com/bcherny/json-schema-to-typescript

TypeScript to JSON schema - https://github.com/YousefED/typescript-json-schema

What I was most curious about was how they achieved run-time type checking in TypeScript.

In this case, it was done by code generation using: https://github.com/bcherny/json-schema-to-typescript

Personally, I'd prefer a way that doesn't involve another build step.

A popular library I've often seen mentioned is io-ts: https://github.com/gcanti/io-ts

One that I've taken a liking to, backed by ajv for JSON schema validation, is: https://github.com/andnp/ValidTyped

Yes, a runtime check in TypeScript is the same as a runtime check in JavaScript.

    function inc(n) {
      if (typeof n === 'number') { return n + 1 }
      throw new Error('Parameter n is not a number');
    }
If you wanted to actually runtime check all types (probably not advised) you could parse the type annotations.

Or you could define your types using something like JSON Schema and validate against that.

https://github.com/bcherny/json-schema-to-typescript

https://github.com/YousefED/typescript-json-schema

Well, you could have one or the other and then just convert a schema to an Interface [1], or vice versa [2].

The reason I like schema-first is because a schema allows you to be way more strict about what's accepted in most cases, e.g. validating an entry via RegEx. It tends to make APIs safer and runtime errors more clear.

[1] https://github.com/bcherny/json-schema-to-typescript

[2] https://github.com/YousefED/typescript-json-schema

Ha! That's the reason I started working on https://github.com/bcherny/json-schema-to-typescript.

For the record, JSON Schemas are quite a bit more expressive than TypeScript interfaces, or even Scala Traits. I actually put together a big list of every JSON Schema constraint that isn't checkable at compile time in TS: https://github.com/bcherny/json-schema-to-typescript#not-exp....