Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kenmori/08c6d46523de4c696c96205deaa65e5e to your computer and use it in GitHub Desktop.
Save kenmori/08c6d46523de4c696c96205deaa65e5e to your computer and use it in GitHub Desktop.
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