A stricter version of Extract that enforces valid extraction shapes.
Extract
StrictExtract tries to prevent accidentally extracting values that could never exist in T.
StrictExtract
T
The source union type
The extraction type (must conform to T)
type Result = StrictExtract<"a" | "b" | "c", "a">;// ^?// "a" Copy
type Result = StrictExtract<"a" | "b" | "c", "a">;// ^?// "a"
type Input = | { type: "a"; value: string } | { type: "b"; value: number };type Result = StrictExtract<Input, { type: "a" }>;// ^?// { type: "a"; value: string } Copy
type Input = | { type: "a"; value: string } | { type: "b"; value: number };type Result = StrictExtract<Input, { type: "a" }>;// ^?// { type: "a"; value: string }
type Input = | { a: string } | { b: number };type Invalid = StrictExtract< Input, //@ts-expect-error - not compatible with any member of Input { c: boolean }>; Copy
type Input = | { a: string } | { b: number };type Invalid = StrictExtract< Input, //@ts-expect-error - not compatible with any member of Input { c: boolean }>;
A stricter version of
Extractthat enforces valid extraction shapes.StrictExtracttries to prevent accidentally extracting values that could never exist inT.