#26401Medium

JSON Schema to TypeScript

Implement the generic type JSONSchema2TS which will return the TypeScript type corresponding to the given JSON schema. Master advanced TypeScript type manipulation in this medium-level challenge on TypeScriptPro.

In this medium-level challenge, you'll implement the generic type JSONSchema2TS which converts a JSON Schema definition into its corresponding TypeScript type, handling primitives, enums, objects with required/optional properties, and arrays.

Challenge Instructions: JSON Schema to TypeScript

Medium

Implement the generic type JSONSchema2TS which will return the TypeScript type corresponding to the given JSON schema.

Additional challenges to handle: additionalProperties oneOf, anyOf, allOf minLength and maxLength

Change the following code to make the test cases pass (no type check errors).

Loading...

Detailed Explanation

type JSONSchema2TS<T> =
  T extends { type: 'string'; enum: infer E extends string[] }
    ? E[number]
    : T extends { type: 'number'; enum: infer E extends number[] }
      ? E[number]
      : T extends { type: 'string' }
        ? string
        : T extends { type: 'number' }
          ? number
          : T extends { type: 'boolean' }
            ? boolean
            : T extends { type: 'object'; properties: infer P; required: infer R extends string[] }
              ? {
                  [K in keyof P as K extends R[number] ? K : never]: JSONSchema2TS<P[K]>
                } & {
                  [K in keyof P as K extends R[number] ? never : K]?: JSONSchema2TS<P[K]>
                } extends infer O
                  ? { [K in keyof O]: O[K] }
                  : never
              : T extends { type: 'object'; properties: infer P }
                ? { [K in keyof P]?: JSONSchema2TS<P[K]> }
                : T extends { type: 'object' }
                  ? Record<string, unknown>
                  : T extends { type: 'array'; items: infer I }
                    ? JSONSchema2TS<I>[]
                    : T extends { type: 'array' }
                      ? unknown[]
                      : never;

How it works:

This challenge helps you understand recursive conditional types and JSON Schema modeling, and how to apply these concepts in real-world scenarios.

This challenge is originally from here.

Share this challenge

Learn the Concepts

Join early, learn faster.

Be the first to access the course, unlock exclusive launch bonuses, and get special early-bird pricing before anyone else.

No spam, unsubscribe at any time. We respect your privacy.

Limited Availability

Only 27 Spots left

Early Access

Get 1 month early access

>75% Off

Pre-Launch discount