What does HackerNews think of json-schema-to-typescript?
Compile JSONSchema to TypeScript type declarations
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.
https://github.com/bcherny/json-schema-to-typescript
It works really well
JSON schema to TypeScript - https://github.com/bcherny/json-schema-to-typescript
TypeScript to JSON schema - https://github.com/YousefED/typescript-json-schema
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
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.
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.
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....