json-schema-to-ts
Advanced tools
Comparing version 0.1.9 to 0.1.10
{ | ||
"name": "json-schema-to-ts", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"description": "Infer typescript types from your JSON schemas!", | ||
@@ -5,0 +5,0 @@ "main": "src/index.ts", |
115
README.md
@@ -89,2 +89,41 @@ # Stop typing twice 🙅♂️ | ||
### Const | ||
```typescript | ||
const fooSchema = { | ||
const: "foo", | ||
} as const; | ||
type Foo = FromSchema<typeof fooSchema>; | ||
// => "foo" | ||
``` | ||
### Enums | ||
```typescript | ||
const enumSchema = { | ||
enum: [true, 42, { foo: "bar" }], | ||
} as const; | ||
type Enum = FromSchema<typeof enumSchema>; | ||
// => true | 42 | { foo: "bar"} | ||
``` | ||
You can also go full circle with typescript `enums`. | ||
```typescript | ||
enum Food { | ||
Pizza = "pizza", | ||
Taco = "taco", | ||
Fries = "Fries", | ||
} | ||
const enumSchema = { | ||
enum: Object.values(Food), | ||
} as const; | ||
type Enum = FromSchema<typeof enumSchema>; | ||
// => Food | ||
``` | ||
### Litterals | ||
@@ -101,2 +140,15 @@ | ||
You can also specify several types: | ||
```typescript | ||
const litteralsSchema = { | ||
type: ["null", "string"], | ||
} as const; | ||
type Litterals = FromSchema<typeof litteralsSchema>; | ||
// => null | string | ||
``` | ||
For `object` and `array` types, properties like `required` or `additionalItems` will also work 🙌 | ||
### Objects | ||
@@ -225,65 +277,2 @@ | ||
### 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 | ||
```typescript | ||
const fooSchema = { | ||
const: "foo", | ||
} as const; | ||
type Foo = FromSchema<typeof fooSchema>; | ||
// => "foo" | ||
``` | ||
### Enums | ||
```typescript | ||
const enumSchema = { | ||
enum: [true, 42, { foo: "bar" }], | ||
} as const; | ||
type Enum = FromSchema<typeof enumSchema>; | ||
// => true | 42 | { foo: "bar"} | ||
``` | ||
`enum` can be used concurrently with `type`. | ||
```typescript | ||
const enumSchema = { | ||
type: "string", | ||
enum: ["foo", "bar", { foo: "bar" }], | ||
} as const; | ||
type Enum = FromSchema<typeof enumSchema>; | ||
// => "foo" | "bar" | ||
``` | ||
### AnyOf | ||
@@ -290,0 +279,0 @@ |
@@ -125,5 +125,5 @@ import { FromWriteableSchema } from "./index"; | ||
*/ | ||
type IsLongerThan<T extends any[], N, R = true> = { | ||
continue: T["length"] extends N ? IsLongerThan<Tail<T>, N> : false; | ||
stop: T["length"] extends N ? R : false; | ||
type IsLongerThan<T extends any[], N, R = false> = { | ||
continue: T["length"] extends N ? true : IsLongerThan<Tail<T>, N>; | ||
stop: T["length"] extends N ? true : R; | ||
}[T extends [any, ...any[]] ? "continue" : "stop"]; | ||
@@ -130,0 +130,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26265
291