IsUnion<T>Checks whether a type T is a union type.
When to use
Use to branch type-level logic depending on whether a type is a union.
Details
- Compares
[T]against[UnionToIntersection<T>]. If they differ,Tmust be a union. - Returns
trueifTis a union of two or more members. - Returns
falsefor single types,never, orany.
Example (Detecting union types)
import type { Types } from "effect"
type Yes = Types.IsUnion<"a" | "b"> // true
type No = Types.IsUnion<string> // falsetypesUnionToIntersection
Source effect/Types.ts:8881 lines
export type type IsUnion<T> = [T] extends [
UnionToIntersection<T>
]
? false
: true
Checks whether a type T is a union type.
When to use
Use to branch type-level logic depending on whether a type is a union.
Details
- Compares
[T] against [UnionToIntersection<T>]. If they differ, T
must be a union.
- Returns
true if T is a union of two or more members.
- Returns
false for single types, never, or any.
Example (Detecting union types)
import type { Types } from "effect"
type Yes = Types.IsUnion<"a" | "b"> // true
type No = Types.IsUnion<string> // false
IsUnion<function (type parameter) T in type IsUnion<T>T> = [function (type parameter) T in type IsUnion<T>T] extends [type UnionToIntersection<T> = (
T extends any ? (x: T) => any : never
) extends (x: infer R) => any
? R
: never
Transforms a union type into an intersection type.
When to use
Use to combine all members of a union into a single type with all their
properties. This is useful in advanced generic code where you need to merge
union variants.
Details
- Uses distributive conditional types and contra-variant inference.
- If the union members are incompatible (e.g.
string | number), the
result is never.
Example (Converting a union to an intersection)
import type { Types } from "effect"
type Union = { a: string } | { b: number }
type Result = Types.UnionToIntersection<Union>
// { a: string } & { b: number }
UnionToIntersection<function (type parameter) T in type IsUnion<T>T>] ? false : true