Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change JTDSchemaType derivation of enums #2359

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/types/jtd-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ export type JTDSchemaType<T, D extends Record<string, unknown> = Record<string,
: // enums - only accepts union of string literals
// TODO we can't actually check that everything in the union was specified
true extends IsEnum<Exclude<T, null>>
? {enum: EnumString<Exclude<T, null>>[]}
? // eslint-disable-next-line @typescript-eslint/array-type
{enum: ReadonlyArray<EnumString<Exclude<T, null>>>}
: // arrays - only accepts arrays, could be array of unions to be resolved later
true extends IsElements<Exclude<T, null>>
? T extends readonly (infer E)[]
Expand Down
12 changes: 11 additions & 1 deletion spec/types/jtd-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,17 @@ describe("JTDSchemaType", () => {
const enumerateString: JTDSchemaType<"a" | "b"> = {type: "string"}
const enumerateNull: JTDSchemaType<"a" | "b" | null> = {enum: ["a", "b"], nullable: true}

void [enumerate, enumerateMissing, enumerateNumber, enumerateString, enumerateNull]
const constEnumSchema = {enum: ["a", "b"]} as const
const enumerateConst: JTDSchemaType<"a" | "b"> = constEnumSchema

void [
enumerate,
enumerateMissing,
enumerateNumber,
enumerateString,
enumerateNull,
enumerateConst,
]
})

it("should typecheck elements schemas", () => {
Expand Down