Documentation - v0.27.1
    Preparing search index...

    Function hasValueAtKey

    • Returns a function that can be used to filter down objects to the ones that have a specific value V under a key k.

      Type Parameters

      • const K extends string | number | symbol
      • const V

      Parameters

      Returns <T>(
          a: T & { [k in string | number | symbol]: unknown },
      ) => a is T & { [k in string | number | symbol]: V }

      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.