exclude
Returns a function that you can use as the argument for Array.prototype.filter to exclude one or more primitive values from an array.
Practical example
import { exclude } from "tsafe/exclude";
type Circle = {
type: "circle";
radius: number;
};
type Square = {
type: "square";
sideLength: number;
};
type Shape = Circle | Square;
declare const shapes: Shape[];
//Assumes we want to do something for every Circle
shapes
.map(shape => (shape.type === "circle" ? shape : null))
.filter(exclude(null))
.forEach(circle => {
//Here circle is of type Circle
//if we had used .filter(shape=> shape.type === "circle")
//it would be functionally the same but circle would be of type
//Shape
});Basic examples
You can also exclude more than on element:
Last updated
Was this helpful?