Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Handling for union when you do not want to use type guard

Handling for union when you do not want to use type guard

You need to type gurd this

type Uni = "A" | "B"

type Fa = {
    contentType : Uni
}

const a: Fa["contentType"] = "A"
a // a is "A" | "B"

Make a separate mold from the beginning

type Uni2<T> = T extends "A" ? "A" : "B"

type Fa2<T extends Uni> = {
    contentType : Uni2<T>
}

const a2:Fa2<"A">["contentType"] = "A"
a2 // "A"

Playground Link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment