ts-data-forge
    Preparing search index...

    Function filter

    • Keeps the entries whose value the predicate accepts, leaving the values themselves untouched.

      Type Behavior: the surviving keys are the keys of the source record, made optional — the predicate may reject any of them. A record keyed by an index signature stays total instead, because it names no specific key that could go missing.

      Passing a type guard narrows the value type of the result, which is what Object.fromEntries(Object.entries(record).filter(guard)) loses.

      Key handling: keys reach the callback the way Object.entries yields them, so a numeric key 1 arrives as '1'. Symbol-keyed properties are not enumerated by Object.entries; they are dropped from both the result and its type.

      Type Parameters

      • const R extends UnknownRecord

        The type of the source record

      • V2 extends unknown

        The type the guard narrows values to

      Parameters

      • record: R

        The record whose entries are filtered

      • predicate: (
            value: R[keyof R],
            key: ToString<RelaxedExclude<keyof R, symbol>>,
        ) => value is V2

        Called once per own enumerable string key, with the value and the key

      Returns FilteredRecord<
          Readonly<
              { [K in string
              | number
              | symbol as K extends symbol ? never : K]: V2 },
          >,
      >

      A new record holding only the accepted entries

      const scores = { alice: 1, bob: 2 } as const;

      const passing = Obj.filter(scores, (value) => value > 1);

      assert.deepStrictEqual(passing, { bob: 2 });

      // The key is available as the second argument.
      const notAlice = Obj.filter(scores, (_value, key) => key !== 'alice');

      assert.deepStrictEqual(notAlice, { bob: 2 });

      // A type guard narrows the value type of the result.
      const mixed: UnknownRecord = { a: 1, b: 'x' } as const;

      const strings = Obj.filter(mixed, isString);

      assert.deepStrictEqual(strings, { b: 'x' });
    • Keeps the entries whose value the predicate accepts, leaving the values themselves untouched.

      Type Behavior: the surviving keys are the keys of the source record, made optional — the predicate may reject any of them. A record keyed by an index signature stays total instead, because it names no specific key that could go missing.

      Passing a type guard narrows the value type of the result, which is what Object.fromEntries(Object.entries(record).filter(guard)) loses.

      Key handling: keys reach the callback the way Object.entries yields them, so a numeric key 1 arrives as '1'. Symbol-keyed properties are not enumerated by Object.entries; they are dropped from both the result and its type.

      Type Parameters

      • const R extends UnknownRecord

        The type of the source record

      Parameters

      • record: R

        The record whose entries are filtered

      • predicate: (value: R[keyof R], key: ToString<RelaxedExclude<keyof R, symbol>>) => boolean

        Called once per own enumerable string key, with the value and the key

      Returns FilteredRecord<R>

      A new record holding only the accepted entries

      const scores = { alice: 1, bob: 2 } as const;

      const passing = Obj.filter(scores, (value) => value > 1);

      assert.deepStrictEqual(passing, { bob: 2 });

      // The key is available as the second argument.
      const notAlice = Obj.filter(scores, (_value, key) => key !== 'alice');

      assert.deepStrictEqual(notAlice, { bob: 2 });

      // A type guard narrows the value type of the result.
      const mixed: UnknownRecord = { a: 1, b: 'x' } as const;

      const strings = Obj.filter(mixed, isString);

      assert.deepStrictEqual(strings, { b: 'x' });