Let JSON play nicely with Typescript using ts-json-validator.

Why?

Naturally, all of the code you write is typed perfectly. But you’re not in charge of all that pesky data that comes from other places.

JSON.parse returns type any, which mangles all of your hard-earned strictness.

JSON validators are great, but they usually require you to define two things: the validation function and the Typescript type to go along with it. These can get out of sync and are generally a pain to maintain. JSON schema is a terrific idea, but the schemas are often tricky to write and even trickier to understand.

ts-json-validator allows you to define everything in one place. It generates a compliant JSON schema, a Typescript type that matches objects that can be parsed by that schema, and provides a typesafe parse that throws if the JSON you get doesn’t match the type you’re expecting.

See the readme or install from npm if you don’t care about how the library works and just want to use it.

How?

A gif showing type-hints from ts-json-validator