type File = { type: "image", imageUrl: string } | { type: "pdf", pdfUrl: string };
const files: File[] = [];
const imageFiles = files.filter(file => file.type === "image");
files[0].type // In this case, TS will still treat it as `"image" | "pdf"`
const filesWithUrl = files.filter(hasValueKey("type", "image" as const));
files[0].type // TS will now know that this is "image"
files[0].imageUrl // TS will know this is present, because already it excluded the other union members.
Returns a function that can be used to filter down objects to the ones that have a specific value V under a key
k
.