HackerReposA FIELD GUIDE TO OPEN SOURCE
ProSponsor the shelf♧ 0
← Back to top all-time
YousefED
typescript-json-schema
TypeScript2016–2023
YousefED / typescript-json-schema

typescript-json-schema

Generate json-schema from your Typescript sources

17Distinct people
20Mentions
2Explicit recommenders
3.3KGitHub stars
REPOSITORY AT A GLANCE

What to know before exploring

What it is

Generate json-schema from your Typescript sources

GitHub

Where it is used

No documented product use was verified in the last source check.

What people are saying

For anyone using both TypeScript and JSON schemas, but wanting to use TypeScript as the source of truth, I highly recommend the following library:…

bertrand-caron used a recommendation phrase. 2 indexed comments match. This is a phrase match, not a verdict.

No classified warning is attached to this page. Read the discussions before treating silence as approval.

Also in these discussions

Mentioned in the same Hacker News threads. That is not proof they are substitutes.

This week on Hacker News

Nov 7–Nov 14 UTC · Direct repository links and separately labeled related project stories. Indexed through 11/14/2026, 3:03:16 PM UTC.

No direct repository links were found in the indexed HN discussions over the past seven days. Older discussions are available below.

Mentions over time

HN mentions by year20 dated mentions

Observed mentions: 2016-08-01 18:48:02 UTC to 2023-03-07 12:26:40 UTC.

02462016-01-01 00:00:00 UTC to 2017-01-01 00:00:00 UTC (exclusive): 1 mentions; 0 submissions, 1 comments20162017-01-01 00:00:00 UTC to 2018-01-01 00:00:00 UTC (exclusive): 6 mentions; 1 submissions, 5 comments2018-01-01 00:00:00 UTC to 2019-01-01 00:00:00 UTC (exclusive): 3 mentions; 0 submissions, 3 comments2019-01-01 00:00:00 UTC to 2020-01-01 00:00:00 UTC (exclusive): 2 mentions; 0 submissions, 2 comments20192020-01-01 00:00:00 UTC to 2021-01-01 00:00:00 UTC (exclusive): 5 mentions; 0 submissions, 5 comments2021-01-01 00:00:00 UTC to 2022-01-01 00:00:00 UTC (exclusive): 2 mentions; 0 submissions, 2 comments2022-01-01 00:00:00 UTC to 2023-01-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2023-01-01 00:00:00 UTC to 2024-01-01 00:00:00 UTC (exclusive): 1 mentions; 0 submissions, 1 comments2023

Counts come from the repository’s dated source evidence. Index cutoff: 2026-11-02 08:16:59 UTC.

View mention counts and date ranges
UTC ranges include their start and exclude their end. Select a period to filter the source evidence.
PeriodStart (UTC)End (exclusive, UTC)MentionsSubmissionsComments
20162016-01-01 00:00:00 UTC2017-01-01 00:00:00 UTC101
20172017-01-01 00:00:00 UTC2018-01-01 00:00:00 UTC615
20182018-01-01 00:00:00 UTC2019-01-01 00:00:00 UTC303
20192019-01-01 00:00:00 UTC2020-01-01 00:00:00 UTC202
20202020-01-01 00:00:00 UTC2021-01-01 00:00:00 UTC505
20212021-01-01 00:00:00 UTC2022-01-01 00:00:00 UTC202
20222022-01-01 00:00:00 UTC2023-01-01 00:00:00 UTC000
20232023-01-01 00:00:00 UTC2024-01-01 00:00:00 UTC101

What Hacker News says

20 sources
bertrand-caron·Mar 7, 2023Recommendation phrase
For anyone using both TypeScript and JSON schemas, but wanting to use TypeScript as the source of truth, I highly recommend the following library: [ts-json-schema-generator](https://github.com/YousefED/typescript-json-schema). It does exactly what it says in the box: turns your TypeScript `types` / `interface` into machine-readable JSON schemas. The library has a few open issues (does not deal well with some edge cases of composing Omit<> on sum types, and does not support dynamic (const) keys), but compared to manually writing JSON schemas, it's been amazing! EDIT: I should add that the library supports adding further type constraints that are supported by JSON Schema but not by TS by using JSDoc (for instance, pattern matching on strings, ranges on numbers, etc.).
In Parse, don't validate (2019)
Thank you - yes, I've dabbled with io-ts, though I'm still learning how to wield its power. > Opt-in runtime checks for SQL queries Ah right, I do see in the repo for PgTyped that it's on the roadmap. That does sound useful! I'm hoping that one of these days, TypeScript will support a more generic runtime type check, somehow closer to the language/TSC-level rather than userland libraries, plugins/transforms, or a parallel compiler/build step. At the moment, I use a fork of typescript-json-schema ¹ for my purposes. ¹ https://github.com/YousefED/typescript-json-schema
In Show HN: PgTyped – Typesafe SQL in TypeScript and Postgres
1. Use discriminated union types: https://www.typescriptlang.org/docs/handbook/advanced-types.... - TypeScript these days sticks with the runtime semantics of JavaScript, and discriminated unions are a very natural way to express algebraic data types in JS. 2. Use https://github.com/YousefED/typescript-json-schema to generate a JSON schema, then use a json schema validator to do runtime validation. That's how https://github.com/Polymer/tachometer handles config file parsing and it works very well.
In TypeScript 3.7: The Biggest Features and How to Use Them
This is a very powerful approach because there are json schema libraries available in all major languages. TypeScript is a better source for truth because it can represent things json schema can't. It is possible to generate a json schema from a TypeScript definition (see https://github.com/YousefED/typescript-json-schema), which means TypeScript can be the single source of truth for API definitions, be used directly in the frontend and, in json schema form, used to test directly against API responses.
In Using JSON Schema to Document, Test, and Debug APIs
jaklfdjkasf·Dec 3, 2018Recommendation phrase
This is very cool, but I highly recommend using a standard like JSON schema instead. It will make your life easier long term. The initial hurdle of learning may be harder, but lots of libraries/languages support JSON schema. If you are using typescript, I highly recommend using a generator to create JSON schema definitions. This one has worked really well recently https://github.com/YousefED/typescript-json-schema
In ForgJs – A lightweight object validator
An alternative I'm a fan of: using typescript-json-schema: https://github.com/YousefED/typescript-json-schema To turn TypeScript interfaces into JSON schemas, then evaluating incoming JSON using whatever JSON schema validation library you want. It's a tradeoff though, as this replaces the need for "t.TypeOf" with some kind of separate build step that creates the schemas. I still prefer it though, as it allows me to use standard TS interfaces rather than any kind of custom format.
In Io-to: both static and dynamic type checking for JSON API data in TypeScript
No, there's nothing wrong with that and it's usually significantly more readable than JSON Schema. Good idea! Of course there are use cases that JSON Schema works better for (eg most other replies you got) but I fully agree that TypeScript type definitions are a great way to specify the structure of JSON data. To make it feel more language-agnostic, you could consider using type aliases instead: type Person = { firstName: string, lastName: string, age?: number // Age in years } This is equivalent to the interface you described, but potentially less confusing to readers who don't know what an "interface" is (eg because they mostly used Ruby or C++). You don't even need to tell people that it's TypeScript - this could be a perfectly sensible schema language for JSON. A bit like what RELAX NG[0] is for XML. A
Read the rest
t my company we actually use typescript-json-schema[1] so we can specify the structure of our API payloads in TS instead of JSON Schema and still benefit from JSON Schema's better tool support. [0] https://en.wikipedia.org/wiki/RELAX_NG#Compact_syntax [1] https://github.com/YousefED/typescript-json-schema
In JSON Schema
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
In JavaScript for People Who Hate JavaScript

Showing the 15 most recent of 20 sources. Enable JavaScript to search and browse every indexed mention.

MAKE YOURSELF AT HOME

A few shortcuts

/
Search the collection
?
Open this guide
Esc
Close dialogs and navigation

Your saved repositories and interests stay in this browser.