Skip to content

Commit

Permalink
allows optional enums (#694)
Browse files Browse the repository at this point in the history
  • Loading branch information
bagbag committed May 6, 2021
1 parent 403d5a7 commit ad647b5
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/utils.ts
Expand Up @@ -203,7 +203,9 @@ export type Assign<T, U> = Simplify<U & Omit<T, keyof U>>
* A schema for enum structs.
*/

export type EnumSchema<T extends string | number> = { [K in T]: K }
export type EnumSchema<T extends string | number | undefined> = {
[K in NonNullable<T>]: K
}

/**
* Check if a type is an exact match.
Expand Down Expand Up @@ -307,14 +309,14 @@ export type If<B extends Boolean, Then, Else> = B extends true ? Then : Else
* A schema for any type of struct.
*/

export type StructSchema<T> = [T] extends [string]
? [T] extends [IsMatch<T, string>]
export type StructSchema<T> = [T] extends [string | undefined]
? [T] extends [IsMatch<T, string | undefined>]
? null
: [T] extends [IsUnion<T>]
? EnumSchema<T>
: T
: [T] extends [number]
? [T] extends [IsMatch<T, number>]
: [T] extends [number | undefined]
? [T] extends [IsMatch<T, number | undefined>]
? null
: [T] extends [IsUnion<T>]
? EnumSchema<T>
Expand Down

0 comments on commit ad647b5

Please sign in to comment.