json-schema-to-ts
Advanced tools
Comparing version 1.4.3 to 1.5.0
import { JSONSchema6Definition, JSONSchema6 } from "json-schema"; | ||
import { Replace } from "../utils"; | ||
export declare type JSONSchema6DefinitionWithoutInterface = JSONSchema6Definition extends infer S ? S extends JSONSchema6 ? Replace<S, "const" | "enum", unknown> : S : never; | ||
export declare type JSONSchema6DefinitionWithoutInterface = JSONSchema6Definition extends infer S ? S extends JSONSchema6 ? Replace<S, "const" | "enum" | "not", unknown> : S : never; |
import { Get } from "../utils"; | ||
export declare type EnumType = "enum"; | ||
export declare type Enum<V> = { | ||
type: "enum"; | ||
type: EnumType; | ||
values: V; | ||
@@ -6,0 +6,0 @@ }; |
@@ -13,3 +13,4 @@ import { Get } from "../utils"; | ||
import { Error, ErrorType } from "./error"; | ||
export declare type MetaType = AnyType | NeverType | ConstType | EnumType | PrimitiveType | ArrType | TupleType | ObjectType | UnionType | IntersectionType | ErrorType; | ||
import { Exclusion, ExclusionType, ResolveExclusion } from "./exclusion"; | ||
export declare type MetaType = AnyType | NeverType | ConstType | EnumType | PrimitiveType | ArrType | TupleType | ObjectType | UnionType | IntersectionType | ExclusionType | ErrorType; | ||
export declare type Resolve<T, D = Exclude<T, undefined>> = { | ||
@@ -26,4 +27,5 @@ any: ResolveAny; | ||
intersection: ResolveIntersection<D>; | ||
exclusion: ResolveExclusion<D>; | ||
error: never; | ||
}[Get<D, "type"> extends MetaType ? Get<D, "type"> : "error"]; | ||
export { Any, Never, Const, Enum, Primitive, Arr, Tuple, Object, Union, Intersection, Error, }; | ||
export { Any, Never, Const, Enum, Primitive, Arr, Tuple, Object, Union, Intersection, Exclusion, Error, }; |
@@ -7,2 +7,4 @@ import { Get } from "../../utils"; | ||
import { IntersectTuple } from "./tuple"; | ||
import { IntersectUnion } from "./union"; | ||
import { IntersectExclusion } from "./exclusion"; | ||
import { ClearIntersections, Intersect } from "./index"; | ||
@@ -19,3 +21,4 @@ export declare type ClearArrIntersections<A> = Arr<ClearIntersections<Values<A>>>; | ||
object: Never; | ||
union: Intersect<B, A>; | ||
union: IntersectUnion<B, A>; | ||
exclusion: IntersectExclusion<B, A>; | ||
intersection: Error<"Cannot intersect intersection">; | ||
@@ -22,0 +25,0 @@ error: B; |
@@ -5,2 +5,4 @@ import { Get, IsObject } from "../../utils"; | ||
import { Values, Required, IsOpen, OpenProps } from "../object"; | ||
import { IntersectUnion } from "./union"; | ||
import { IntersectExclusion } from "./exclusion"; | ||
import { Intersect } from "./index"; | ||
@@ -16,3 +18,4 @@ export declare type IntersectConst<A, B> = { | ||
object: ToObject<A, B>; | ||
union: Intersect<B, A>; | ||
union: IntersectUnion<B, A>; | ||
exclusion: IntersectExclusion<B, A>; | ||
intersection: Error<"Cannot intersect intersection">; | ||
@@ -19,0 +22,0 @@ error: B; |
@@ -5,2 +5,4 @@ import { Get } from "../../utils"; | ||
import { IntersectConst } from "./const"; | ||
import { IntersectUnion } from "./union"; | ||
import { IntersectExclusion } from "./exclusion"; | ||
import { Intersect } from "./index"; | ||
@@ -16,3 +18,4 @@ export declare type IntersectEnum<A, B> = { | ||
object: FilterExtendingResolved<A, B>; | ||
union: Intersect<B, A>; | ||
union: IntersectUnion<B, A>; | ||
exclusion: IntersectExclusion<B, A>; | ||
intersection: Error<"Cannot intersect intersection">; | ||
@@ -19,0 +22,0 @@ error: B; |
@@ -10,2 +10,3 @@ import { DoesExtend, Get } from "../../utils"; | ||
import { ClearUnionIntersections, IntersectUnion } from "./union"; | ||
import { ClearExclusionIntersections, IntersectExclusion } from "./exclusion"; | ||
export declare type IntersectionType = "intersection"; | ||
@@ -31,4 +32,5 @@ export declare type Intersection<L, R> = { | ||
union: ClearUnionIntersections<T>; | ||
intersection: Intersect<ClearIntersections<Left<T>>, ClearIntersections<Right<T>>>; | ||
exclusion: ClearExclusionIntersections<T>; | ||
error: T; | ||
intersection: Intersect<ClearIntersections<Left<T>>, ClearIntersections<Right<T>>>; | ||
errorMissingType: Error<"Missing type property">; | ||
@@ -46,5 +48,6 @@ }[Get<T, "type"> extends MetaType ? Get<T, "type"> : "errorMissingType"]; | ||
union: IntersectUnion<A, B>; | ||
intersection: Error<"Cannot intersect intersection">; | ||
exclusion: IntersectExclusion<A, B>; | ||
error: A; | ||
errorMissingType: Error<"Missing type property">; | ||
intersection: Error<"Cannot intersect intersection">; | ||
}[Get<A, "type"> extends MetaType ? Get<A, "type"> : "errorMissingType"]; |
@@ -7,2 +7,3 @@ import { Get, And } from "../../utils"; | ||
import { DistributeIntersection } from "./union"; | ||
import { IntersectExclusion } from "./exclusion"; | ||
import { ClearIntersections, Intersect } from "./index"; | ||
@@ -26,2 +27,3 @@ export declare type ClearObjectIntersections<A, V = ClearObjectValuesIntersections<Values<A>>, N = NeverKeys<V>, O = ClearIntersections<OpenProps<A>>> = Required<A> extends Exclude<Required<A>, N> ? Object<{ | ||
intersection: Error<"Cannot intersect intersection">; | ||
exclusion: IntersectExclusion<B, A>; | ||
error: B; | ||
@@ -28,0 +30,0 @@ errorTypeProperty: Error<"Missing type property">; |
@@ -6,2 +6,3 @@ import { Get } from "../../utils"; | ||
import { IntersectEnum } from "./enum"; | ||
import { IntersectExclusion } from "./exclusion"; | ||
import { Intersect } from "."; | ||
@@ -19,4 +20,5 @@ export declare type IntersectPrimitive<A, B> = { | ||
intersection: Error<"Cannot intersect intersection">; | ||
exclusion: IntersectExclusion<B, A>; | ||
error: B; | ||
errorTypeProperty: Error<"Missing type property">; | ||
}[Get<B, "type"> extends MetaType ? Get<B, "type"> : "errorTypeProperty"]; |
@@ -8,2 +8,3 @@ import { Get, Head, Tail, Prepend, Reverse, And } from "../../utils"; | ||
import { DistributeIntersection } from "./union"; | ||
import { IntersectExclusion } from "./exclusion"; | ||
import { ClearIntersections, Intersect } from "."; | ||
@@ -26,2 +27,3 @@ export declare type ClearTupleIntersections<T, O = ClearIntersections<OpenProps<T>>> = Tuple<ClearTupleValuesIntersections<Values<T>>, O extends Never ? false : IsOpen<T>, O>; | ||
intersection: Error<"Cannot intersect intersection">; | ||
exclusion: IntersectExclusion<B, A>; | ||
error: B; | ||
@@ -28,0 +30,0 @@ errorTypeProperty: Error<"Missing type property">; |
@@ -17,2 +17,3 @@ import { Get } from "../../utils"; | ||
union: DistributeIntersection<A, B>; | ||
exclusion: DistributeIntersection<A, B>; | ||
intersection: Error<"Cannot intersect intersection">; | ||
@@ -19,0 +20,0 @@ error: B; |
@@ -10,2 +10,3 @@ import { Primitive, Any, Never } from "../meta-types"; | ||
import { ParseAllOfSchema } from "./allOf"; | ||
import { ParseNotSchema } from "./not"; | ||
export declare type ParseSchema<S> = { | ||
@@ -26,4 +27,5 @@ any: Any; | ||
allOf: ParseAllOfSchema<S>; | ||
not: ParseNotSchema<S>; | ||
}[InferSchemaType<S>]; | ||
declare type InferSchemaType<S> = S extends true | string ? "any" : S extends false ? "never" : "allOf" extends keyof S ? "allOf" : "oneOf" extends keyof S ? "oneOf" : "anyOf" extends keyof S ? "anyOf" : "enum" extends keyof S ? "enum" : "const" extends keyof S ? "const" : "type" extends keyof S ? S["type"] extends any[] ? "mixed" : S["type"] extends "null" ? "null" : S["type"] extends "boolean" ? "boolean" : S["type"] extends "integer" | "number" ? "number" : S["type"] extends "string" ? "string" : S["type"] extends "object" ? "object" : S["type"] extends "array" ? "array" : "never" : "any"; | ||
declare type InferSchemaType<S> = S extends true | string ? "any" : S extends false ? "never" : "not" extends keyof S ? "not" : "allOf" extends keyof S ? "allOf" : "oneOf" extends keyof S ? "oneOf" : "anyOf" extends keyof S ? "anyOf" : "enum" extends keyof S ? "enum" : "const" extends keyof S ? "const" : "type" extends keyof S ? S["type"] extends any[] ? "mixed" : S["type"] extends "null" ? "null" : S["type"] extends "boolean" ? "boolean" : S["type"] extends "integer" | "number" ? "number" : S["type"] extends "string" ? "string" : S["type"] extends "object" ? "object" : S["type"] extends "array" ? "array" : "never" : "any"; | ||
export {}; |
{ | ||
"name": "json-schema-to-ts", | ||
"version": "1.4.3", | ||
"version": "1.5.0", | ||
"description": "Infer typescript types from your JSON schemas!", | ||
@@ -20,3 +20,3 @@ "main": "lib/index.d.ts", | ||
"babel-jest": "^26.1.0", | ||
"jest": "^26.1.0", | ||
"jest": "^26.6.3", | ||
"typescript": "^3.9.6" | ||
@@ -23,0 +23,0 @@ }, |
@@ -447,6 +447,45 @@ <img src="assets/header-round-medium.png" width="100%" align="center" /> | ||
### Not and If-Then-Else | ||
### Not | ||
For the same reason as `oneOf` (missing refinment types), I feel like implementing the `not` and the `if/then/else` keywords in `FromSchema` would lead into a rabbit hole... | ||
```typescript | ||
const tupleSchema = { | ||
type: "array", | ||
items: [{ const: 1 }, { const: 2 }], | ||
additionalItems: false, | ||
not: { | ||
const: [1], | ||
}, | ||
} as const; | ||
But I may be wrong! If you think that it can be implemented, feel free to [open an issue](https://github.com/ThomasAribart/json-schema-to-ts/issues) 🤗 | ||
type Tuple = FromSchema<typeof tupleSchema>; | ||
// => [] | [1, 2] | ||
``` | ||
```typescript | ||
const primitiveTypeSchema = { | ||
not: { | ||
type: ["array", "object"], | ||
}, | ||
} as const; | ||
type PrimitiveType = FromSchema<typeof primitiveTypeSchema>; | ||
// => null | boolean | number | string | ||
``` | ||
Keep in mind that the `not` type computation uses the `Exclude` utility type, which suffers from Typescript's limitations: | ||
```typescript | ||
const goodLanguageSchema = { | ||
type: "string", | ||
not: { | ||
enum: ["Bummer", "Silly", "Lazy sod !"], | ||
}, | ||
} as const; | ||
type GoodLanguage = FromSchema<typeof goodLanguageSchema>; | ||
// => string | ||
``` | ||
### If-Then-Else | ||
The `if/then/else` keyword is not implemented yet. |
Sorry, the diff of this file is not supported yet
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
54285
57
865
491