Comparing version 0.0.0-dev.20201229023359 to 0.0.0-dev.20210203015357
166
index.d.ts
declare namespace AsTypedInternal { | ||
interface SchemaBase { | ||
type SchemaBase = { | ||
$id?: string; | ||
$ref?: string; | ||
type?: string | string[]; | ||
title?: string; | ||
description?: string; | ||
nullable?: boolean; | ||
default?: any; | ||
examples?: any[]; | ||
} | ||
} & ( | ||
| { | ||
type?: string | string[]; | ||
title?: string; | ||
description?: string; | ||
nullable?: boolean; | ||
default?: any; | ||
examples?: any[]; | ||
} | ||
| { $ref: string } | ||
| { oneOf: SchemaBase[] } | ||
| { allOf: SchemaBase[] } | ||
| { | ||
if: SchemaBase; | ||
then: SchemaBase; | ||
else?: SchemaBase; | ||
} | ||
); | ||
interface DefinitionsBase { | ||
[name: string]: SchemaBase; | ||
} | ||
type SchemaWithDefinitions< | ||
SchemaDefinitions extends DefinitionsBase | ||
> = SchemaBase & { | ||
definitions: SchemaDefinitions; | ||
}; | ||
type SchemaDeclaration<Type> = SchemaBase & { | ||
type: Type; | ||
$id?: string; | ||
}; | ||
@@ -235,36 +234,18 @@ | ||
| ResolveRecursiveInternal<Omit<SchemaType, "type"> & { type: Rest }> | ||
: never; | ||
: SchemaType extends OneOf<infer Inner> | ||
? ResolveRecursiveInternal<Inner> | ||
: SchemaType extends AnyOf<infer Inner> | ||
? ResolveRecursiveInternal<Inner> | ||
: SchemaType extends AllOf<infer Inner> | ||
? ResolveRecursiveInternal<UnionToIntersection<Inner>> | ||
: SchemaType extends IfThenElseSchema<infer If, infer Then, infer Else> | ||
? ResolveRecursiveInternal<(If & Then) | Else> | ||
: SchemaType; | ||
// High order resolution changes the schema before resolving it to typed | ||
type ResolveRecursive<SchemaType> = ResolveRecursiveInternal<SchemaType>; | ||
type ResolveHighOrder< | ||
SchemaToResolve extends SchemaBase | ||
> = SchemaToResolve extends IfThenElseSchema<infer If, infer Then, infer Else> | ||
? (If & Then) | Else | ||
: SchemaToResolve extends OneOf<infer Inner> | ||
? Inner | ||
: SchemaToResolve extends AllOf<infer Inner> | ||
? UnionToIntersection<Inner> | ||
: SchemaToResolve extends AnyOf<infer Inner> | ||
? Inner | ||
: SchemaToResolve; | ||
type MapPropsToRefs<RootSchema, Props> = { | ||
[name in keyof Props]: ResolveRefs<RootSchema, Props[name]>; | ||
}; | ||
type ResolveRecursive<SchemaType> = ResolveRecursiveInternal< | ||
ResolveHighOrder<SchemaType> | ||
>; | ||
type MapPropsToRefs< | ||
RootSchema, | ||
Props, | ||
Definitions extends DefinitionsBase | ||
> = Definitions extends { [name: string]: SchemaBase } | ||
? { | ||
[name in keyof Props]: ResolveRefs< | ||
RootSchema, | ||
Props[name], | ||
Definitions | ||
>; | ||
} | ||
: never; | ||
type ResolveIfThenElseRefs< | ||
@@ -274,8 +255,7 @@ RootSchema, | ||
Then extends SchemaBase, | ||
Else extends SchemaBase, | ||
Definitions extends DefinitionsBase | ||
Else extends SchemaBase | ||
> = SchemaBase & { | ||
if: ResolveRefs<RootSchema, If, Definitions>; | ||
then: ResolveRefs<RootSchema, Then, Definitions>; | ||
else: ResolveRefs<RootSchema, Else, Definitions>; | ||
if: ResolveRefs<RootSchema, If>; | ||
then: ResolveRefs<RootSchema, Then>; | ||
else: ResolveRefs<RootSchema, Else>; | ||
}; | ||
@@ -285,6 +265,5 @@ | ||
RootSchema, | ||
ValueType extends SchemaBase, | ||
Definitions extends DefinitionsBase | ||
ValueType extends SchemaBase | ||
> = SchemaDeclaration<"array"> & { | ||
items: ResolveRefs<RootSchema, ValueType, Definitions>; | ||
items: ResolveRefs<RootSchema, ValueType>; | ||
}; | ||
@@ -295,7 +274,6 @@ | ||
Tuple extends SchemaBase[], | ||
Additional extends SchemaBase, | ||
Definitions extends DefinitionsBase | ||
Additional extends SchemaBase | ||
> = SchemaDeclaration<"array"> & { | ||
items: ResolveRefs<RootSchema, Tuple, Definitions>; | ||
additionalItems: ResolveRefs<RootSchema, Additional, Definitions>; | ||
items: ResolveRefs<RootSchema, Tuple>; | ||
additionalItems: ResolveRefs<RootSchema, Additional>; | ||
}; | ||
@@ -306,6 +284,5 @@ | ||
ValueType extends SchemaBase, | ||
Operator extends string, | ||
Definitions extends DefinitionsBase | ||
Operator extends string | ||
> = { | ||
[name in Operator]: Array<ResolveRefs<RootSchema, ValueType, Definitions>>; | ||
[name in Operator]: Array<ResolveRefs<RootSchema, ValueType>>; | ||
}; | ||
@@ -316,19 +293,5 @@ | ||
ValueType extends SchemaBase, | ||
Operator extends string, | ||
Definitions extends DefinitionsBase | ||
> = { [name in Operator]: ResolveRefs<RootSchema, ValueType, Definitions> }; | ||
Operator extends string | ||
> = { [name in Operator]: ResolveRefs<RootSchema, ValueType> }; | ||
type ResolveDefinitions<RootSchema, Definitions extends DefinitionsBase> = { | ||
[DefinitionName in keyof Definitions]: ResolveRefs< | ||
RootSchema, | ||
Definitions[DefinitionName], | ||
Definitions | ||
>; | ||
}; | ||
type ExtractDefinitionsById<Definitions extends DefinitionsBase> = { | ||
[key in Definitions[keyof Definitions]["$id"] & | ||
string]: Definitions[keyof Definitions]; | ||
}; | ||
type ResolvePath< | ||
@@ -345,40 +308,33 @@ Schema, | ||
type LocateId<Candidates, Id extends string> = Candidates extends { $id: Id } | ||
? Candidates | ||
: never; | ||
type ResolveRefs< | ||
RootSchema, | ||
SchemaToResolve, | ||
Definitions extends DefinitionsBase | ||
SchemaToResolve | ||
> = SchemaToResolve extends RefSchema<infer RefId> | ||
? RefId extends `#/${infer Path}` | ||
? ResolveRefs<RootSchema, ResolvePath<RootSchema, Path>, Definitions> | ||
: Definitions[RefId] | ||
? ResolveRefs<RootSchema, ResolvePath<RootSchema, Path>> | ||
: RootSchema extends { definitions: infer Definitions } | ||
? ResolveRefs<RootSchema, LocateId<Definitions[keyof Definitions], RefId>> | ||
: never | ||
: SchemaToResolve extends ObjectSchema<infer Props, infer Required> | ||
? ObjectSchema<MapPropsToRefs<RootSchema, Props, Definitions>, Required> | ||
? ObjectSchema<MapPropsToRefs<RootSchema, Props>, Required> | ||
: SchemaToResolve extends TupleSchema<infer Tuple, infer Additional> | ||
? Tuple extends SchemaBase[] | ||
? ResolveTupleRefs<RootSchema, Tuple, Additional, Definitions> | ||
? ResolveTupleRefs<RootSchema, Tuple, Additional> | ||
: never | ||
: SchemaToResolve extends ArraySchema<infer ValueType> | ||
? ResolveArrayRefs<RootSchema, ValueType, Definitions> | ||
? ResolveArrayRefs<RootSchema, ValueType> | ||
: SchemaToResolve extends CombinerSchema<infer ValueType, infer Operator> | ||
? ResolveCombinerRefs<RootSchema, ValueType, Operator, Definitions> | ||
? ResolveCombinerRefs<RootSchema, ValueType, Operator> | ||
: SchemaToResolve extends OperatorSchema<infer ValueType, infer Operator> | ||
? ResolveOperatorRefs<RootSchema, ValueType, Operator, Definitions> | ||
? ResolveOperatorRefs<RootSchema, ValueType, Operator> | ||
: SchemaToResolve extends IfThenElseSchema<infer If, infer Then, infer Else> | ||
? ResolveIfThenElseRefs<RootSchema, If, Then, Else, Definitions> | ||
? ResolveIfThenElseRefs<RootSchema, If, Then, Else> | ||
: SchemaToResolve; | ||
type ResolveRootSchemaDefinitions< | ||
Schema | ||
> = Schema extends SchemaWithDefinitions<infer D> | ||
? ResolveDefinitions<Schema, ExtractDefinitionsById<D>> | ||
: {}; | ||
type ResolveRefsForRootSchema<RootSchema> = ResolveRefs< | ||
RootSchema, | ||
RootSchema, | ||
ResolveRootSchemaDefinitions<RootSchema> | ||
>; | ||
type ResolveRootSchema<RootSchema> = ResolveRecursive< | ||
ResolveRefsForRootSchema<RootSchema> | ||
ResolveRefs<RootSchema, RootSchema> | ||
>; | ||
@@ -385,0 +341,0 @@ |
{ | ||
"name": "as-typed", | ||
"version": "0.0.0-dev.20201229023359", | ||
"version": "0.0.0-dev.20210203015357", | ||
"description": "Static TypeScript types from a literal JSONSchema type", | ||
@@ -5,0 +5,0 @@ "types": "index.d.ts", |
28
test.ts
@@ -33,13 +33,2 @@ import { AsTyped } from "./index"; | ||
assert( | ||
_ as AsTyped<{ | ||
definitions: { | ||
str1: { $id: "def"; $ref: "def2" }; | ||
str2: { $id: "def2"; type: "string" }; | ||
}; | ||
$ref: "def"; | ||
}>, | ||
_ as string | ||
); | ||
assert( | ||
_ as AsTyped<{ type: "array"; items: { type: "number" } }>, | ||
@@ -132,2 +121,19 @@ _ as number[] | ||
_ as AsTyped<{ | ||
oneOf: [{ type: "string" }, { type: "number" }, { type: "boolean" }]; | ||
}>, | ||
_ as string | number | boolean | ||
); | ||
assert( | ||
_ as AsTyped<{ | ||
oneOf: [ | ||
{ type: "string" }, | ||
{ oneOf: [{ type: "number" }, { type: "boolean" }] } | ||
]; | ||
}>, | ||
_ as string | number | boolean | ||
); | ||
assert( | ||
_ as AsTyped<{ | ||
oneOf: [{ type: "string" }, { type: "array"; items: { type: "string" } }]; | ||
@@ -134,0 +140,0 @@ }>, |
34676
888