From 372961d6b17f38305b156403ce9e2f1bcdea671e Mon Sep 17 00:00:00 2001 From: Erik Brinkman Date: Wed, 31 Mar 2021 22:19:49 -0400 Subject: [PATCH] improve JSONSchemaType errors typescript seems to report errors with the last member of a valid union first, so this rearranges the type so the simple union types come first, and the type that represents the meaty types comes last. This should help make errors like the one in #1521 more clear. --- lib/types/json-schema.ts | 47 ++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/lib/types/json-schema.ts b/lib/types/json-schema.ts index be68547ff..20fa4b7f8 100644 --- a/lib/types/json-schema.ts +++ b/lib/types/json-schema.ts @@ -24,6 +24,30 @@ interface StringKeywords { } export type JSONSchemaType = ( + | // these two unions allow arbitrary unions of types + { + anyOf: readonly JSONSchemaType[] + } + | { + oneOf: readonly JSONSchemaType[] + } + // this union allows for { type: (primitive)[] } style schemas + | ({ + type: (T extends number + ? JSONType<"number" | "integer", _partial> + : T extends string + ? JSONType<"string", _partial> + : T extends boolean + ? JSONType<"boolean", _partial> + : never)[] + } & (T extends number + ? NumberKeywords + : T extends string + ? StringKeywords + : T extends boolean + ? unknown + : never)) + // this covers "normal" types; it's last so typescript looks to it first for errors | ((T extends number ? { type: JSONType<"number" | "integer", _partial> @@ -91,29 +115,6 @@ export type JSONSchemaType = ( else?: PartialSchema not?: PartialSchema }) - // these two unions allow arbitrary unions of types - | { - anyOf: readonly JSONSchemaType[] - } - | { - oneOf: readonly JSONSchemaType[] - } - // this union allows for { type: (primitive)[] } style schemas - | ({ - type: (T extends number - ? JSONType<"number" | "integer", _partial> - : T extends string - ? JSONType<"string", _partial> - : T extends boolean - ? JSONType<"boolean", _partial> - : never)[] - } & (T extends number - ? NumberKeywords - : T extends string - ? StringKeywords - : T extends boolean - ? unknown - : never)) ) & { [keyword: string]: any $id?: string