json-schema-to-ts
Advanced tools
Comparing version 0.1.5 to 0.1.6
{ | ||
"name": "json-schema-to-ts", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "Infer typescript types from your JSON schemas!", | ||
@@ -5,0 +5,0 @@ "main": "src/index.ts", |
@@ -69,3 +69,4 @@ # Stop typing twice 🙅♂️ | ||
type Pet = FromSchema<typeof petSchema>; // => Will work 🙌 | ||
type Pet = FromSchema<typeof petSchema>; | ||
// => Will work 🙌 | ||
``` | ||
@@ -96,3 +97,4 @@ | ||
type Litteral = FromSchema<typeof litteralSchema>; // => null, boolean, string or number | ||
type Litteral = FromSchema<typeof litteralSchema>; | ||
// => null, boolean, string or number | ||
``` | ||
@@ -112,3 +114,4 @@ | ||
type Object = FromSchema<typeof objectSchema>; // => { foo: string, bar?: number } | ||
type Object = FromSchema<typeof objectSchema>; | ||
// => { foo: string, bar?: number } | ||
``` | ||
@@ -124,3 +127,4 @@ | ||
type Array = FromSchema<typeof arraySchema>; // => string[] | ||
type Array = FromSchema<typeof arraySchema>; | ||
// => string[] | ||
``` | ||
@@ -136,3 +140,4 @@ | ||
type Tuple = FromSchema<typeof tupleSchema>; // => [] | [boolean] | [boolean, string] | [boolean, string, ...any[]] | ||
type Tuple = FromSchema<typeof tupleSchema>; | ||
// => [] | [boolean] | [boolean, string] | [boolean, string, ...any[]] | ||
``` | ||
@@ -149,6 +154,35 @@ | ||
type Tuple = FromSchema<typeof tupleSchema>; // => [] | [boolean] | [boolean, string] | ||
const tupleInstance: Tuple = [true, "string", 42]; // => Will error ❌ | ||
type Tuple = FromSchema<typeof tupleSchema>; | ||
// => [] | [boolean] | [boolean, string] | ||
``` | ||
### Multiple Types | ||
```typescript | ||
const multipleTypesSchema = { | ||
type: ["null", "string"], | ||
} as const; | ||
type Tuple = FromSchema<typeof multipleTypesSchema>; | ||
// => null | string | ||
``` | ||
Other properties like `required` or `additionalItems` will also work 🙌 | ||
```typescript | ||
const multipleTypesSchema = { | ||
type: ["array", "object"], | ||
items: [{ type: "string" }], | ||
additionalItems: false, | ||
properties: { | ||
name: { type: "string" }, | ||
age: { type: "number" }, | ||
}, | ||
required: ["name"], | ||
} as const; | ||
type Tuple = FromSchema<typeof multipleTypesSchema>; | ||
// => [] | [string] | { name: string, age?: number } | ||
``` | ||
### Const | ||
@@ -161,3 +195,4 @@ | ||
type Foo = FromSchema<typeof fooSchema>; // => "foo" | ||
type Foo = FromSchema<typeof fooSchema>; | ||
// => "foo" | ||
``` | ||
@@ -172,3 +207,4 @@ | ||
type Enum = FromSchema<typeof enumSchema>; // => true | 42 | { foo: "bar"} | ||
type Enum = FromSchema<typeof enumSchema>; | ||
// => true | 42 | { foo: "bar"} | ||
``` | ||
@@ -184,5 +220,21 @@ | ||
type Enum = FromSchema<typeof enumSchema>; // => "foo" | "bar" | ||
type Enum = FromSchema<typeof enumSchema>; | ||
// => "foo" | "bar" | ||
``` | ||
If used in concurrency with `const`, the `enum` keyword will be omitted. | ||
### AnyOf | ||
```typescript | ||
const anyOfSchema = { | ||
anyOf: [ | ||
{ type: "string" }, | ||
{ | ||
type: "array", | ||
items: { type: "string" }, | ||
}, | ||
], | ||
} as const; | ||
type AnyOf = FromSchema<typeof fooSchema>; | ||
// => string | string[] | ||
``` |
import { FromAnyOfSchema } from "./anyOf"; | ||
import { FromEnumSchema } from "./enum"; | ||
import { FromConstSchema } from "./const"; | ||
import { FromMultipleSchema } from "./multiple"; | ||
import { FromObjectSchema } from "./object"; | ||
@@ -18,10 +19,11 @@ import { FromArraySchema } from "./array"; | ||
const: FromConstSchema<S>; | ||
multiple: FromMultipleSchema<S>; | ||
null: null; | ||
boolean: boolean; | ||
number: number; | ||
string: string; | ||
number: number; | ||
object: FromObjectSchema<S>; | ||
array: FromArraySchema<S>; | ||
structureError: "TypeError: Invalid schema structure"; | ||
typeError: 'TypeError: type value should be "null", "boolean", "integer", "number", "string", "object" or "array"'; | ||
typeError: "TypeError: Invalid type value. Did you forget to use the 'as const' directive?"; | ||
}[InferSchemaType<S>]; | ||
@@ -40,3 +42,5 @@ | ||
: "type" extends keyof S | ||
? S["type"] extends "null" | ||
? S["type"] extends any[] | ||
? "multiple" | ||
: S["type"] extends "null" | ||
? "null" | ||
@@ -43,0 +47,0 @@ : S["type"] extends "boolean" |
16358
21
321
232