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";typeCircle= { type:"circle"; radius:number;};typeSquare= { type:"square"; sideLength:number;};typeShape=Circle|Square;declareconstshapes:Shape[];//Assumes we want to do something for every Circleshapes.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
import { exclude } from"tsafe/exclude";constarr= ["a","b","c","d"] asconst;constnewArr=arr.filter(exclude("a"));//type of newArr is ("b" | "c" | "d")[]//value of newArr is ["b", "c", "d"]
You can also exclude more than on element:
import { exclude } from"tsafe/exclude";constarr= ["a","b","c","d"] asconst;constnewArr=arr.filter(exclude(["a","b"]));//type of newArr is ("c" | "d")[]//value of newArr is ["c", "d"]