Skip to content

Commit

Permalink
feat: nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
slont committed Oct 23, 2023
1 parent 490eb8c commit d327d66
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions lib/types/json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,14 @@ export type RequiredMembers<T> = StrictNullChecksWrapper<
UncheckedRequiredMembers<T>
>

type Nullable<T> = undefined extends T
? {
nullable: true
const?: null // any non-null value would fail `const: null`, `null` would fail any other value in const
enum?: Readonly<(T | null)[]> // `null` must be explicitly included in "enum" for `null` to pass
default?: T | null
}
: {
nullable?: false
const?: T
enum?: Readonly<T[]>
default?: T
}
type Nullable<T> = {
nullable: true
const?: null // any non-null value would fail `const: null`, `null` would fail any other value in const
enum?: Readonly<(T | null)[]> // `null` must be explicitly included in "enum" for `null` to pass
default?: T | null
} | {
nullable?: false
const?: T
enum?: Readonly<T[]>
default?: T
}

0 comments on commit d327d66

Please sign in to comment.