Skip to content

Bug with unions: when we use them with optional, null, array, etc., they turn into simple strings #135

@fdaciuk

Description

@fdaciuk

if I have a union like this on my schema, the type will be a union:

const schema = S.union(["Win", "Draw", "Loss"])
type Schema = S.Output<typeof schema> // "Win" | "Draw" | "Loss"

That is correct.

But if the type is optional, nullish, array, etc., the union becomes a simple string:

const schema = S.optional(S.union(["Win", "Draw", "Loss"]))
type Schema = S.Output<typeof schema> // string | undefined
const schema = S.nullish(S.union(["Win", "Draw", "Loss"]))
type Schema = S.Output<typeof schema> // string | null | undefined
const schema = S.array(S.union(["Win", "Draw", "Loss"]))
type Schema = S.Output<typeof schema> // string[]

The only way I found to not get this behaviour is using S.schema in any strings of my union:

const schema = S.optional(S.union([S.schema("Win"), S.schema("Draw"), S.schema("Loss")]))
type Schema = S.Output<typeof schema> // "Win" | "Draw" | "Loss" | undefined

Is this a bug or it is the intended behaviour? I'm using [email protected].

Metadata

Metadata

Assignees

No one assigned

    Labels

    typescriptRelated to TypeScript APIwontfixThis will not be worked on

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions