mapped type that ensures a string union has an element of a specific string literal

You should swap sides on extends in type definition:

type HasReady<S extends { status: string }> = { status: "ready" } extends S 
  ? S
  : never;

Explanation:

{ status: "ready" | "bananas" } extends { status: "ready" } ? { status: "ready" | "bananas" } : never

–>

({ status: "ready" } extends { status: "ready" } ? { status: "ready" | "bananas" } : never) & ({ status: "bananas" } extends { status: "ready" } ? { status: "ready" | "bananas" } : never)

–>

{ status: "ready" | "bananas" } & never

–>

never

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top