discriminate
The discriminate() method discriminates an object by the value of a property (type guard for unions).
Interactive example
Syntax
Classic signature
typescript
function discriminate<
GenericInput extends object,
GenericKey extends keyof GenericInput,
GenericInput extends EligibleEqual
>(
input: GenericInput,
key: GenericKey,
value: (MaybeArray<(GenericInput & Extract<GenericInput[GenericKey], EligibleEqual>)> | MaybeArray<Extract<GenericInput[GenericKey], EligibleEqual>>)
): input is Extract<GenericInput, {
[Prop in GenericKey]: GenericInput;
}>Curried signature
typescript
function discriminate<
GenericInput extends object,
GenericKey extends keyof GenericInput,
GenericInput extends EligibleEqual
>(
key: GenericKey,
value: ...
): (input: GenericInput) => input is Extract<GenericInput, {
[Prop in GenericKey]: GenericInput;
}>Parameters
input: The object to discriminate (often a union type).key: The key of the discriminant property.input: The expected value (or array of values) for discrimination.
Return value
A boolean that acts as a type guard to narrow the union type.
See also
deepDiscriminate- Discriminates by a deep propertyhasKeys- Checks for the presence of keys
