MappingByDiscriminator
Example:
type Car = {
kind: "car",
...
};
type Truck = {
kind: "truck",
...
};
type Vehicle = Car | Truck;
Then
type VehiclesByKind = MappingByDiscriminator<Vehicle,"kind">;
Will produce :
type VehiclesByKind = {
car: Car[],
truck: Truck[],
}
Mapping union type on discriminator
using the UnionMemberByDiscriminator and GetProperty described below,
type MappingByDiscriminator< E extends object, D extends keyof E > =
GetPropertyType<E,D> extends string | number ?
{
[d in GetPropertyType<E, D>]: UnionMemberByDiscriminator<E, D, d>[];
}
: never;
Will map types in a union by a discriminator.
GetProperty
Get the type of an object property with generics
type GetProperty<E extends object, P extends keyof E> =
E extends { [key in P]: infer V } ? V : never;
Example:
type Entity = {
kind: 'human' | 'animal',
size: 'microscopic' | 'small' | 'big' | 'giant',
}
Then
type SizeType = GetProperty<Entity,"size">
Will produce
type SizeType = 'microscopic' | 'small' | 'big' | 'giant';
UnionMemberByDiscriminator
Extract a specific type from a union for a given discriminator value (can be used with generics)
type UnionMemberByDiscriminator<
E extends object,
D extends keyof E,
DValue extends string | number
> = E extends {[key in D]:DValue} ? E : never;
Example:
type Car = {
kind: "car",
...
};
type Truck = {
kind: "truck",
...
};
type Vehicle = Car | Truck;
type InferredCar =
UnionMemberByDiscriminator<Vehicle, "kind", "car">
Will produce
type InferredCar = Car;
This is useful in a generic context (see above)
Top comments (2)
Just as a well-organized cheat sheet helps readers quickly compare and understand technical concepts, a clear product comparison can make purchasing decisions much easier. For truck owners researching retractable bed covers, Roll-N-Lock provides a useful comparison of the A-Series and M-Series, highlighting differences in construction, features, security, weather protection, and overall value.
The TypeScript cheat sheet provides a practical quick-reference approach to programming concepts, making it useful for readers who appreciate clear, organized information and efficient solutions. In a similar spirit of choosing purpose-built solutions for everyday needs, BakFlip offers hard-folding truck bed covers designed around protection, functionality, and convenient access. For truck owners researching practical ways to keep their cargo covered while maintaining bed usability, the BakFlip MX4 for the Ford Maverick is worth exploring as a dedicated fit.