Comparing version 0.0.0-dev.20201127230609 to 0.0.0-dev.20201127233233
@@ -270,9 +270,17 @@ declare namespace AsTypedInternal { | ||
type MapPropsToRefs< | ||
RootSchema, | ||
Props, | ||
Definitions extends DefinitionsBase | ||
> = Definitions extends { [name: string]: SchemaBase } | ||
? { [name in keyof Props]: ResolveRefs<Props[name], Definitions> } | ||
? { | ||
[name in keyof Props]: ResolveRefs< | ||
RootSchema, | ||
Props[name], | ||
Definitions | ||
>; | ||
} | ||
: never; | ||
type ResolveIfThenElseRefs< | ||
RootSchema, | ||
If extends SchemaBase, | ||
@@ -283,13 +291,17 @@ Then extends SchemaBase, | ||
> = SchemaBase & { | ||
if: ResolveRefs<If, Definitions>; | ||
then: ResolveRefs<Then, Definitions>; | ||
else: ResolveRefs<Else, Definitions>; | ||
if: ResolveRefs<RootSchema, If, Definitions>; | ||
then: ResolveRefs<RootSchema, Then, Definitions>; | ||
else: ResolveRefs<RootSchema, Else, Definitions>; | ||
}; | ||
type ResolveArrayRefs< | ||
RootSchema, | ||
ValueType extends SchemaBase, | ||
Definitions extends DefinitionsBase | ||
> = SchemaDeclaration<any[]> & { items: ResolveRefs<ValueType, Definitions> }; | ||
> = SchemaDeclaration<any[]> & { | ||
items: ResolveRefs<RootSchema, ValueType, Definitions>; | ||
}; | ||
type ResolveTupleRefs< | ||
RootSchema, | ||
Tuple extends SchemaBase[], | ||
@@ -299,20 +311,25 @@ Additional extends SchemaBase, | ||
> = SchemaDeclaration<any[]> & { | ||
items: ResolveRefs<Tuple, Definitions>; | ||
additionalItems: ResolveRefs<Additional, Definitions>; | ||
items: ResolveRefs<RootSchema, Tuple, Definitions>; | ||
additionalItems: ResolveRefs<RootSchema, Additional, Definitions>; | ||
}; | ||
type ResolveCombinerRefs< | ||
RootSchema, | ||
ValueType extends SchemaBase, | ||
Operator extends string, | ||
Definitions extends DefinitionsBase | ||
> = { [name in Operator]: Array<ResolveRefs<ValueType, Definitions>> }; | ||
> = { | ||
[name in Operator]: Array<ResolveRefs<RootSchema, ValueType, Definitions>>; | ||
}; | ||
type ResolveOperatorRefs< | ||
RootSchema, | ||
ValueType extends SchemaBase, | ||
Operator extends string, | ||
Definitions extends DefinitionsBase | ||
> = { [name in Operator]: ResolveRefs<ValueType, Definitions> }; | ||
> = { [name in Operator]: ResolveRefs<RootSchema, ValueType, Definitions> }; | ||
type ResolveDefinitions<Definitions extends DefinitionsBase> = { | ||
type ResolveDefinitions<RootSchema, Definitions extends DefinitionsBase> = { | ||
[DefinitionName in keyof Definitions]: ResolveRefs< | ||
RootSchema, | ||
Definitions[DefinitionName], | ||
@@ -328,21 +345,35 @@ Definitions | ||
type ResolvePath< | ||
Schema, | ||
Path | ||
> = Path extends `${infer Prop}/${infer PathRest}` | ||
? Prop extends keyof Schema | ||
? ResolvePath<Schema[Prop], PathRest> | ||
: never | ||
: Path extends keyof Schema | ||
? Schema[Path] | ||
: never; | ||
type ResolveRefs< | ||
RootSchema, | ||
SchemaToResolve, | ||
Definitions extends DefinitionsBase | ||
> = SchemaToResolve extends RefSchema<infer RefId> | ||
? Definitions[RefId] | ||
? RefId extends `#/${infer Path}` | ||
? ResolveRefs<RootSchema, ResolvePath<RootSchema, Path>, Definitions> | ||
: Definitions[RefId] | ||
: SchemaToResolve extends ObjectSchema<infer Props, infer Required> | ||
? ObjectSchema<MapPropsToRefs<Props, Definitions>, Required> | ||
? ObjectSchema<MapPropsToRefs<RootSchema, Props, Definitions>, Required> | ||
: SchemaToResolve extends TupleSchema<infer Tuple, infer Additional> | ||
? Tuple extends SchemaBase[] | ||
? ResolveTupleRefs<Tuple, Additional, Definitions> | ||
? ResolveTupleRefs<RootSchema, Tuple, Additional, Definitions> | ||
: never | ||
: SchemaToResolve extends ArraySchema<infer ValueType> | ||
? ResolveArrayRefs<ValueType, Definitions> | ||
? ResolveArrayRefs<RootSchema, ValueType, Definitions> | ||
: SchemaToResolve extends CombinerSchema<infer ValueType, infer Operator> | ||
? ResolveCombinerRefs<ValueType, Operator, Definitions> | ||
? ResolveCombinerRefs<RootSchema, ValueType, Operator, Definitions> | ||
: SchemaToResolve extends OperatorSchema<infer ValueType, infer Operator> | ||
? ResolveOperatorRefs<ValueType, Operator, Definitions> | ||
? ResolveOperatorRefs<RootSchema, ValueType, Operator, Definitions> | ||
: SchemaToResolve extends IfThenElseSchema<infer If, infer Then, infer Else> | ||
? ResolveIfThenElseRefs<If, Then, Else, Definitions> | ||
? ResolveIfThenElseRefs<RootSchema, If, Then, Else, Definitions> | ||
: SchemaToResolve; | ||
@@ -353,3 +384,3 @@ | ||
> = Schema extends SchemaWithDefinitions<infer D> | ||
? ResolveDefinitions<ExtractDefinitionsById<D>> | ||
? ResolveDefinitions<Schema, ExtractDefinitionsById<D>> | ||
: {}; | ||
@@ -359,2 +390,3 @@ | ||
RootSchema, | ||
RootSchema, | ||
ResolveRootSchemaDefinitions<RootSchema> | ||
@@ -361,0 +393,0 @@ >; |
{ | ||
"name": "as-typed", | ||
"version": "0.0.0-dev.20201127230609", | ||
"version": "0.0.0-dev.20201127233233", | ||
"description": "Static TypeScript types from a literal JSONSchema type", | ||
@@ -5,0 +5,0 @@ "types": "index.d.ts", |
@@ -51,10 +51,2 @@ # `as-typed` | ||
## Overview | ||
There are many tools that convert between Typescript and JSON schema. Howewever, all of them do that with a transpilation step. | ||
Ambison uses the deep inference options in typescript 3 to infer the types directly from the JSON schema. | ||
This is not a runtime library - it contains only a single generic type, AsTyped. | ||
AsTyped can take any Json schema and AsTypeds it to a typescript type. | ||
### Primitive types | ||
@@ -117,5 +109,5 @@ | ||
#### Limitations | ||
#### Path references: | ||
Reference by URL ("#/definitions/foo", "other.json/foo") are not supported yet. | ||
- `AsTyped<{definitions: {foo: {type: "number"}}, $ref: "#/definitions/foo"}>` === `number` | ||
@@ -122,0 +114,0 @@ ### not |
19
test.ts
@@ -230,1 +230,20 @@ import { AsTyped } from "./index"; | ||
); | ||
assert( | ||
_ as AsTyped<{ | ||
definitions: { foo: { type: "number" } }; | ||
$ref: "#/definitions/foo"; | ||
}>, | ||
_ as number | ||
); | ||
assert( | ||
_ as AsTyped<{ | ||
definitions: { | ||
str1: { $ref: "#/definitions/str2" }; | ||
str2: { type: "string" }; | ||
}; | ||
$ref: "#/definitions/str1"; | ||
}>, | ||
_ as string | ||
); |
34019
893
134