json-schema-to-ts

Infer TS types from JSON schemas

README

If you use this repo, star it ✨


Stop typing twice 🙅‍♂️


A lot of projects use JSON schemas for runtime data validation along with TypeScript for static type checking.

Their code may look like this:

  1. ```typescript
  2. const dogSchema = {
  3.   type: "object",
  4.   properties: {
  5.     name: { type: "string" },
  6.     age: { type: "integer" },
  7.     hobbies: { type: "array", items: { type: "string" } },
  8.     favoriteFood: { enum: ["pizza", "taco", "fries"] },
  9.   },
  10.   required: ["name", "age"],
  11. };

  12. type Dog = {
  13.   name: string;
  14.   age: number;
  15.   hobbies?: string[];
  16.   favoriteFood?: "pizza" | "taco" | "fries";
  17. };
  18. ```

Both objects carry similar if not exactly the same information. This is a code duplication that can annoy developers and introduce bugs if not properly maintained.

That's when json-schema-to-ts comes to the rescue 💪

FromSchema


The FromSchema method lets you infer TS types directly from JSON schemas:

  1. ```typescript
  2. import { FromSchema } from "json-schema-to-ts";

  3. const dogSchema = {
  4.   type: "object",
  5.   properties: {
  6.     name: { type: "string" },
  7.     age: { type: "integer" },
  8.     hobbies: { type: "array", items: { type: "string" } },
  9.     favoriteFood: { enum: ["pizza", "taco", "fries"] },
  10.   },
  11.   required: ["name", "age"],
  12. } as const;

  13. type Dog = FromSchema<typeof dogSchema>;
  14. // => Will infer the same type as above
  15. ```

Schemas can even be nested, as long as you don't forget the as const statement:

  1. ```typescript
  2. const catSchema = { ... } as const;

  3. const petSchema = {
  4.   anyOf: [dogSchema, catSchema],
  5. } as const;

  6. type Pet = FromSchema<typeof petSchema>;
  7. // => Will work 🙌
  8. ```

The as const statement is used so that TypeScript takes the schema definition to the word (e.g. _true_ is interpreted as the _true_ constant and not widened as _boolean_). It is pure TypeScript and has zero impact on the compiled code.

If you don't mind impacting the compiled code, you can use the asConst util, which simply returns the schema while narrowing its inferred type.

  1. ```typescript
  2. import { asConst } from "json-schema-to-ts";

  3. const dogSchema = asConst({
  4.   type: "object",
  5.   ...
  6. });

  7. type Dog = FromSchema<typeof dogSchema>;
  8. // => Will work as well 🙌
  9. ```

Why use json-schema-to-ts?


If you're looking for runtime validation with added types, libraries like yup, zod or runtypes may suit your needs while being easier to use!

On the other hand, JSON schemas have the benefit of being widely used, more versatile and reusable (swaggers, APIaaS...).

If you prefer to stick to them and can define your schemas in TS instead of JSON (importing JSONs as const is not available yet), then json-schema-to-ts is made for you:

- ✅ Schema validation FromSchema raises TS errors on invalid schemas, based on DefinitelyTyped's definitions
- ✨ No impact on compiled code: json-schema-to-ts only operates in type space. And after all, what's lighter than a dev-dependency?
- 🍸 DRYness: Less code means less embarrassing typos
- 🤝 Real-time consistency: See that string that you used instead of an enum? Or this additionalProperties you confused with additionalItems? Or forgot entirely? Well, json-schema-to-ts does!
- 🔧 Reliability: FromSchema is extensively tested against AJV, and covers all the use cases that can be handled by TS for now\*
- 🏋️‍♂️ Help on complex schemas: Get complex schemas right first time with instantaneous typing feedbacks! For instance, it's not obvious the following schema can never be validated:

  1. ```typescript
  2. const addressSchema = {
  3.   type: "object",
  4.   allOf: [
  5.     {
  6.       properties: {
  7.         street: { type: "string" },
  8.         city: { type: "string" },
  9.         state: { type: "string" },
  10.       },
  11.       required: ["street", "city", "state"],
  12.     },
  13.     {
  14.       properties: {
  15.         type: { enum: ["residential", "business"] },
  16.       },
  17.     },
  18.   ],
  19.   additionalProperties: false,
  20. } as const;
  21. ```

But it is with FromSchema!

  1. ```typescript
  2. type Address = FromSchema<typeof addressSchema>;
  3. // => never 🙌
  4. ```

\*If json-schema-to-ts misses one of your use case, feel free to open an issue 🤗


Table of content


  - Const
  - Enums
  - Nullable
  - Arrays
  - Tuples
  - Objects
  - AnyOf
  - AllOf
  - OneOf
  - Not
  - Compilers
- FAQ

Installation


  1. ``` sh
  2. # npm
  3. npm install --save-dev json-schema-to-ts

  4. # yarn
  5. yarn add --dev json-schema-to-ts
  6. ```

json-schema-to-ts requires TypeScript 4.3+. Using strict mode is required, as well as (apparently) turning off [noStrictGenericChecks](https://www.typescriptlang.org/tsconfig#noStrictGenericChecks).


Use cases


Const


  1. ```typescript
  2. const fooSchema = {
  3.   const: "foo",
  4. } as const;

  5. type Foo = FromSchema<typeof fooSchema>;
  6. // => "foo"
  7. ```

Enums


  1. ```typescript
  2. const enumSchema = {
  3.   enum: [true, 42, { foo: "bar" }],
  4. } as const;

  5. type Enum = FromSchema<typeof enumSchema>;
  6. // => true | 42 | { foo: "bar"}
  7. ```

You can also go full circle with typescript enums.

  1. ```typescript
  2. enum Food {
  3.   Pizza = "pizza",
  4.   Taco = "taco",
  5.   Fries = "fries",
  6. }

  7. const enumSchema = {
  8.   enum: Object.values(Food),
  9. } as const;

  10. type Enum = FromSchema<typeof enumSchema>;
  11. // => Food
  12. ```

Primitive types


  1. ```typescript
  2. const primitiveTypeSchema = {
  3.   type: "null", // "boolean", "string", "integer", "number"
  4. } as const;

  5. type PrimitiveType = FromSchema<typeof primitiveTypeSchema>;
  6. // => null, boolean, string or number
  7. ```

  1. ```typescript
  2. const primitiveTypesSchema = {
  3.   type: ["null", "string"],
  4. } as const;

  5. type PrimitiveTypes = FromSchema<typeof primitiveTypesSchema>;
  6. // => null | string
  7. ```

For more complex types, refinment keywords like required or additionalItems will apply 🙌


Nullable


  1. ```typescript
  2. const nullableSchema = {
  3.   type: "string",
  4.   nullable: true,
  5. } as const;

  6. type Nullable = FromSchema<typeof nullableSchema>;
  7. // => string | null
  8. ```

Arrays


  1. ```typescript
  2. const arraySchema = {
  3.   type: "array",
  4.   items: { type: "string" },
  5. } as const;

  6. type Array = FromSchema<typeof arraySchema>;
  7. // => string[]
  8. ```

Tuples


  1. ```typescript
  2. const tupleSchema = {
  3.   type: "array",
  4.   items: [{ type: "boolean" }, { type: "string" }],
  5. } as const;

  6. type Tuple = FromSchema<typeof tupleSchema>;
  7. // => [] | [boolean] | [boolean, string] | [boolean, string, ...unknown[]]
  8. ```

FromSchema supports the additionalItems keyword:

  1. ```typescript
  2. const tupleSchema = {
  3.   type: "array",
  4.   items: [{ type: "boolean" }, { type: "string" }],
  5.   additionalItems: false,
  6. } as const;

  7. type Tuple = FromSchema<typeof tupleSchema>;
  8. // => [] | [boolean] | [boolean, string]
  9. ```

  1. ```typescript
  2. const tupleSchema = {
  3.   type: "array",
  4.   items: [{ type: "boolean" }, { type: "string" }],
  5.   additionalItems: { type: "number" },
  6. } as const;

  7. type Tuple = FromSchema<typeof tupleSchema>;
  8. // => [] | [boolean] | [boolean, string] | [boolean, string, ...number[]]
  9. ```

...as well as the minItems and maxItems keywords:

  1. ```typescript
  2. const tupleSchema = {
  3.   type: "array",
  4.   items: [{ type: "boolean" }, { type: "string" }],
  5.   minItems: 1,
  6.   maxItems: 2,
  7. } as const;

  8. type Tuple = FromSchema<typeof tupleSchema>;
  9. // => [boolean] | [boolean, string]
  10. ```

Additional items will only work if Typescript's strictNullChecks option is activated


Objects


  1. ```typescript
  2. const objectSchema = {
  3.   type: "object",
  4.   properties: {
  5.     foo: { type: "string" },
  6.     bar: { type: "number" },
  7.   },
  8.   required: ["foo"],
  9. } as const;

  10. type Object = FromSchema<typeof objectSchema>;
  11. // => { [x: string]: unknown; foo: string; bar?: number; }
  12. ```

FromSchema partially supports the additionalProperties and patternProperties keywords:

- additionalProperties can be used to deny additional properties.

  1. ```typescript
  2. const closedObjectSchema = {
  3.   ...objectSchema,
  4.   additionalProperties: false,
  5. } as const;

  6. type Object = FromSchema<typeof closedObjectSchema>;
  7. // => { foo: string; bar?: number; }
  8. ```

- Used on their own, additionalProperties and/or patternProperties can be used to type unnamed properties.

  1. ```typescript
  2. const openObjectSchema = {
  3.   type: "object",
  4.   additionalProperties: {
  5.     type: "boolean",
  6.   },
  7.   patternProperties: {
  8.     "^S": { type: "string" },
  9.     "^I": { type: "integer" },
  10.   },
  11. } as const;

  12. type Object = FromSchema<typeof openObjectSchema>;
  13. // => { [x: string]: string | number | boolean }
  14. ```

- However, when used in combination with the properties keyword, extra properties will always be typed as unknown to avoid conflicts.

Combining schemas


AnyOf


  1. ```typescript
  2. const anyOfSchema = {
  3.   anyOf: [
  4.     { type: "string" },
  5.     {
  6.       type: "array",
  7.       items: { type: "string" },
  8.     },
  9.   ],
  10. } as const;

  11. type AnyOf = FromSchema<typeof anyOfSchema>;
  12. // => string | string[]
  13. ```

FromSchema will correctly infer factored schemas:

  1. ```typescript
  2. const factoredSchema = {
  3.   type: "object",
  4.   properties: {
  5.     bool: { type: "boolean" },
  6.   },
  7.   required: ["bool"],
  8.   anyOf: [
  9.     {
  10.       properties: {
  11.         str: { type: "string" },
  12.       },
  13.       required: ["str"],
  14.     },
  15.     {
  16.       properties: {
  17.         num: { type: "number" },
  18.       },
  19.     },
  20.   ],
  21. } as const;

  22. type Factored = FromSchema<typeof factoredSchema>;
  23. // => {
  24. //  [x:string]: unknown;
  25. //  bool: boolean;
  26. //  str: string;
  27. // } | {
  28. //  [x:string]: unknown;
  29. //  bool: boolean;
  30. //  num?: number;
  31. // }
  32. ```

OneOf


FromSchema will parse the oneOf keyword in the same way as anyOf:

  1. ```typescript
  2. const catSchema = {
  3.   type: "object",
  4.   oneOf: [
  5.     {
  6.       properties: {
  7.         name: { type: "string" },
  8.       },
  9.       required: ["name"],
  10.     },
  11.     {
  12.       properties: {
  13.         color: { enum: ["black", "brown", "white"] },
  14.       },
  15.     },
  16.   ],
  17. } as const;

  18. type Cat = FromSchema<typeof catSchema>;
  19. // => {
  20. //  [x: string]: unknown;
  21. //  name: string;
  22. // } | {
  23. //  [x: string]: unknown;
  24. //  color?: "black" | "brown" | "white";
  25. // }

  26. // => Error will NOT be raised 😱
  27. const invalidCat: Cat = { name: "Garfield" };
  28. ```

AllOf


  1. ```typescript
  2. const addressSchema = {
  3.   type: "object",
  4.   allOf: [
  5.     {
  6.       properties: {
  7.         address: { type: "string" },
  8.         city: { type: "string" },
  9.         state: { type: "string" },
  10.       },
  11.       required: ["address", "city", "state"],
  12.     },
  13.     {
  14.       properties: {
  15.         type: { enum: ["residential", "business"] },
  16.       },
  17.     },
  18.   ],
  19. } as const;

  20. type Address = FromSchema<typeof addressSchema>;
  21. // => {
  22. //   [x: string]: unknown;
  23. //   address: string;
  24. //   city: string;
  25. //   state: string;
  26. //   type?: "residential" | "business";
  27. // }
  28. ```

Not


Exclusions require heavy computations, that can sometimes be aborted by Typescript and end up in any inferred types. For this reason, they are not activated by default: You can opt-in with the parseNotKeyword option.

  1. ```typescript
  2. const tupleSchema = {
  3.   type: "array",
  4.   items: [{ const: 1 }, { const: 2 }],
  5.   additionalItems: false,
  6.   not: {
  7.     const: [1],
  8.   },
  9. } as const;

  10. type Tuple = FromSchema<typeof tupleSchema, { parseNotKeyword: true }>;
  11. // => [] | [1, 2]
  12. ```

  1. ```typescript
  2. const primitiveTypeSchema = {
  3.   not: {
  4.     type: ["array", "object"],
  5.   },
  6. } as const;

  7. type PrimitiveType = FromSchema<
  8.   typeof primitiveTypeSchema,
  9.   { parseNotKeyword: true }
  10. >;
  11. // => null | boolean | number | string
  12. ```

In objects and tuples, the exclusion will propagate to properties/items if it can collapse on a single one.

  1. ```typescript
  2. // 👍 Can be propagated on "animal" property
  3. const petSchema = {
  4.   type: "object",
  5.   properties: {
  6.     animal: { enum: ["cat", "dog", "boat"] },
  7.   },
  8.   not: {
  9.     properties: { animal: { const: "boat" } },
  10.   },
  11.   required: ["animal"],
  12.   additionalProperties: false,
  13. } as const;

  14. type Pet = FromSchema<typeof petSchema, { parseNotKeyword: true }>;
  15. // => { animal: "cat" | "dog" }
  16. ```

  1. ```typescript
  2. // ❌ Cannot be propagated
  3. const petSchema = {
  4.   type: "object",
  5.   properties: {
  6.     animal: { enum: ["cat", "dog"] },
  7.     color: { enum: ["black", "brown", "white"] },
  8.   },
  9.   not: {
  10.     const: { animal: "cat", color: "white" },
  11.   },
  12.   required: ["animal", "color"],
  13.   additionalProperties: false,
  14. } as const;

  15. type Pet = FromSchema<typeof petSchema, { parseNotKeyword: true }>;
  16. // => { animal: "cat" | "dog", color: "black" | "brown" | "white" }
  17. ```

As some actionable keywords are not yet parsed, exclusions that resolve to never are granted the benefit of the doubt and omitted. For the moment, FromSchema assumes that you are not crafting unvalidatable exclusions.

  1. ```typescript
  2. const oddNumberSchema = {
  3.   type: "number",
  4.   not: { multipleOf: 2 },
  5. } as const;

  6. type OddNumber = FromSchema<typeof oddNumberSchema, { parseNotKeyword: true }>;
  7. // => should and will resolve to "number"

  8. const incorrectSchema = {
  9.   type: "number",
  10.   not: { bogus: "option" },
  11. } as const;

  12. type Incorrect = FromSchema<typeof incorrectSchema, { parseNotKeyword: true }>;
  13. // => should resolve to "never" but will still resolve to "number"
  14. ```

Also, keep in mind that TypeScript misses refinment types:

  1. ```typescript
  2. const goodLanguageSchema = {
  3.   type: "string",
  4.   not: {
  5.     enum: ["Bummer", "Silly", "Lazy sod !"],
  6.   },
  7. } as const;

  8. type GoodLanguage = FromSchema<
  9.   typeof goodLanguageSchema,
  10.   { parseNotKeyword: true }
  11. >;
  12. // => string
  13. ```

If/Then/Else


For the same reason as the Not keyword, conditions parsing is not activated by default: You can opt-in with the parseIfThenElseKeywords option.

  1. ```typescript
  2. const petSchema = {
  3.   type: "object",
  4.   properties: {
  5.     animal: { enum: ["cat", "dog"] },
  6.     dogBreed: { enum: Object.values(DogBreed) },
  7.     catBreed: { enum: Object.values(CatBreed) },
  8.   },
  9.   required: ["animal"],
  10.   if: {
  11.     properties: {
  12.       animal: { const: "dog" },
  13.     },
  14.   },
  15.   then: {
  16.     required: ["dogBreed"],
  17.     not: { required: ["catBreed"] },
  18.   },
  19.   else: {
  20.     required: ["catBreed"],
  21.     not: { required: ["dogBreed"] },
  22.   },
  23.   additionalProperties: false,
  24. } as const;

  25. type Pet = FromSchema<typeof petSchema, { parseIfThenElseKeywords: true }>;
  26. // => {
  27. //  animal: "dog";
  28. //  dogBreed: DogBreed;
  29. //  catBreed?: CatBreed | undefined
  30. // } | {
  31. //  animal: "cat";
  32. //  catBreed: CatBreed;
  33. //  dogBreed?: DogBreed | undefined
  34. // }
  35. ```

☝️ FromSchema computes the resulting type as (If ∩ Then) ∪ (¬If ∩ Else). While correct in theory, remember that the not keyword is not perfectly assimilated, which may become an issue in some complex schemas.


Definitions


  1. ```typescript
  2. const userSchema = {
  3.   type: "object",
  4.   properties: {
  5.     name: { $ref: "#/$defs/name" },
  6.     age: { $ref: "#/$defs/age" },
  7.   },
  8.   required: ["name", "age"],
  9.   additionalProperties: false,
  10.   $defs: {
  11.     name: { type: "string" },
  12.     age: { type: "integer" },
  13.   },
  14. } as const;

  15. type User = FromSchema<typeof userSchema>;
  16. // => {
  17. //  name: string;
  18. //  age: number;
  19. // }
  20. ```

☝️ Wether in definitions or references, FromSchema will not work on recursive schemas for now.


References


Unlike run-time validator classes like AJV, TS types cannot withhold internal states. Thus, they cannot keep any identified schemas in memory.

But you can hydrate them via the references option:

  1. ```typescript
  2. const userSchema = {
  3.   $id: "http://example.com/schemas/user.json",
  4.   type: "object",
  5.   properties: {
  6.     name: { type: "string" },
  7.     age: { type: "integer" },
  8.   },
  9.   required: ["name", "age"],
  10.   additionalProperties: false,
  11. } as const;

  12. const usersSchema = {
  13.   type: "array",
  14.   items: {
  15.     $ref: "http://example.com/schemas/user.json",
  16.   },
  17. } as const;

  18. type Users = FromSchema<
  19.   typeof usersSchema,
  20.   { references: [typeof userSchema] }
  21. >;
  22. // => {
  23. //  name: string;
  24. //  age: string;
  25. // }[]

  26. const anotherUsersSchema = {
  27.   $id: "http://example.com/schemas/users.json",
  28.   type: "array",
  29.   items: { $ref: "user.json" },
  30. } as const;
  31. // => Will work as well 🙌
  32. ```

Deserialization


You can specify deserialization patterns with the deserialize option:

  1. ```typescript
  2. const userSchema = {
  3.   type: "object",
  4.   properties: {
  5.     name: { type: "string" },
  6.     email: {
  7.       type: "string",
  8.       format: "email",
  9.     },
  10.     birthDate: {
  11.       type: "string",
  12.       format: "date-time",
  13.     },
  14.   },
  15.   required: ["name", "email", "birthDate"],
  16.   additionalProperties: false,
  17. } as const;

  18. type Email = string & { brand: "email" };

  19. type User = FromSchema<
  20.   typeof userSchema,
  21.   {
  22.     deserialize: [
  23.       {
  24.         pattern: {
  25.           type: "string";
  26.           format: "email";
  27.         };
  28.         output: Email;
  29.       },
  30.       {
  31.         pattern: {
  32.           type: "string";
  33.           format: "date-time";
  34.         };
  35.         output: Date;
  36.       }
  37.     ];
  38.   }
  39. >;
  40. // => {
  41. //  name: string;
  42. //  email: Email;
  43. //  birthDate: Date;
  44. // }
  45. ```

Typeguards


You can use FromSchema to implement your own typeguard:

  1. ```typescript
  2. import { FromSchema, Validator } from "json-schema-to-ts";

  3. // It's important to:
  4. // - Explicitely type your validator as Validator
  5. // - Use FromSchema as the default value of a 2nd generic first
  6. const validate: Validator = <S extends JSONSchema, T = FromSchema<S>>(
  7.   schema: S,
  8.   data: unknown
  9. ): data is T => {
  10.   const isDataValid: boolean = ... // Implement validation here
  11.   return isDataValid;
  12. };

  13. const petSchema = { ... } as const
  14. let data: unknown;
  15. if (validate(petSchema, data)) {
  16.   const { name, ... } = data; // data is typed as Pet 🙌
  17. }
  18. ```

If needed, you can provide FromSchema options and additional validation options to the Validator type:

  1. ```typescript
  2. type FromSchemaOptions = { parseNotKeyword: true };
  3. type ValidationOptions = [{ fastValidate: boolean }]

  4. const validate: Validator<FromSchemaOptions, ValidationOptions> = <
  5.   S extends JSONSchema,
  6.   T = FromSchema<S, FromSchemaOptions>
  7. >(
  8.   schema: S,
  9.   data: unknown,
  10.   ...validationOptions: ValidationOptions
  11. ): data is T => { ... };
  12. ```

json-schema-to-ts also exposes two helpers to write type guards. They don't impact the code that you wrote (they simply return it), but turn it into type guards.

You can use them to wrap either [validators](#validator) or [compilers](#compiler).

Validators


A validator is a function that receives a schema plus some data, and returns true if the data is valid compared to the schema, false otherwise.

You can use the wrapValidatorAsTypeGuard helper to turn validators into type guards. Here is an implementation with ajv:

  1. ```typescript
  2. import Ajv from "ajv";
  3. import { $Validator, wrapValidatorAsTypeGuard } from "json-schema-to-ts";

  4. const ajv = new Ajv();

  5. // The initial validator definition is up to you
  6. // ($Validator is prefixed with $ to differ from resulting type guard)
  7. const $validate: $Validator = (schema, data) => ajv.validate(schema, data);

  8. const validate = wrapValidatorAsTypeGuard($validate);

  9. const petSchema = { ... } as const;

  10. let data: unknown;
  11. if (validate(petSchema, data)) {
  12.   const { name, ... } = data; // data is typed as Pet 🙌
  13. }
  14. ```

If needed, you can provide FromSchema options and additional validation options as generic types:

  1. ```typescript
  2. type FromSchemaOptions = { parseNotKeyword: true };
  3. type ValidationOptions = [{ fastValidate: boolean }]

  4. const $validate: $Validator<ValidationOptions> = (
  5.   schema,
  6.   data,
  7.   ...validationOptions // typed as ValidationOptions
  8. ) => { ... };

  9. // validate will inherit from ValidationOptions
  10. const validate = wrapValidatorAsTypeGuard($validate);

  11. // with special FromSchemaOptions
  12. // (ValidationOptions needs to be re-provided)
  13. const validate = wrapValidatorAsTypeGuard<
  14.   FromSchemaOptions,
  15.   ValidationOptions
  16. >($validate);
  17. ```

Compilers


A compiler is a function that takes a schema as an input and returns a data validator for this schema as an output.

You can use the wrapCompilerAsTypeGuard helper to turn compilers into type guard builders. Here is an implementation with ajv:

  1. ```typescript
  2. import Ajv from "ajv";
  3. import { $Compiler, wrapCompilerAsTypeGuard } from "json-schema-to-ts";

  4. // The initial compiler definition is up to you
  5. // ($Compiler is prefixed with $ to differ from resulting type guard)
  6. const $compile: $Compiler = (schema) => ajv.compile(schema);

  7. const compile = wrapCompilerAsTypeGuard($compile);

  8. const petSchema = { ... } as const;

  9. const isPet = compile(petSchema);

  10. let data: unknown;
  11. if (isPet(data)) {
  12.   const { name, ... } = data; // data is typed as Pet 🙌
  13. }
  14. ```

If needed, you can provide FromSchema options, additional compiling and validation options as generic types:

  1. ```typescript
  2. type FromSchemaOptions = { parseNotKeyword: true };
  3. type CompilingOptions = [{ fastCompile: boolean }];
  4. type ValidationOptions = [{ fastValidate: boolean }];

  5. const $compile: $Compiler<CompilingOptions, ValidationOptions> = (
  6.   schema,
  7.   ...compilingOptions // typed as CompilingOptions
  8. ) => {
  9.   ...

  10.   return (
  11.     data,
  12.     ...validationOptions // typed as ValidationOptions
  13.   ) => { ...  };
  14. };

  15. // compile will inherit from all options
  16. const compile = wrapCompilerAsTypeGuard($compile);

  17. // with special FromSchemaOptions
  18. // (options need to be re-provided)
  19. const compile = wrapCompilerAsTypeGuard<
  20.   FromSchemaOptions,
  21.   CompilingOptions,
  22.   ValidationOptions
  23. >($compile);
  24. ```

Frequently Asked Questions


- [Does json-schema-to-ts work on _.json_ file schemas?](./documentation/FAQs/does-json-schema-to-ts-work-on-json-file-schemas.md)
- [Can I assign JSONSchema to my schema and use FromSchema at the same time?](./documentation/FAQs/can-i-assign-jsonschema-to-my-schema-and-use-fromschema-at-the-same-time.md)
- [Will json-schema-to-ts impact the performances of my IDE/compiler?](./documentation/FAQs/will-json-schema-to-ts-impact-the-performances-of-my-ide-compiler.md)
- [I get a type instantiation is excessively deep and potentially infinite error, what should I do?](./documentation/FAQs/i-get-a-type-instantiation-is-excessively-deep-and-potentially-infinite-error-what-should-i-do.md)