ts-data-forge
    Preparing search index...

    Function map

    • Transforms every value of a record, leaving the key set untouched.

      Type Behavior: the result is a mapped type over keyof R, so the keys — and their optional modifiers — survive verbatim: { a: number; b?: number } maps to { a: V2; b?: V2 }, and a record with an index signature stays total rather than becoming Partial.

      This is what Object.fromEntries(Object.entries(record).map(...)) cannot express: an entries array carries no evidence that it still covers every key of the record it came from, so its element type is all the return type can be built from. Going through a mapped type instead states the invariant that a value-only transform preserves the key set.

      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

        The type the callback returns for every key

      Parameters

      • record: R

        The record whose values are transformed

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

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

      Returns TsDataForgeInternals.MappedRecord<R, V2>

      A new record with the same keys as record and the mapped values

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

      const doubled = Obj.map(scores, (value) => value * 2);

      assert.deepStrictEqual(doubled, { alice: 2, bob: 4 });

      // The key is available as the second argument.
      const labelled = Obj.map(scores, (value, key) => `${key}=${value}`);

      assert.deepStrictEqual(labelled, { alice: 'alice=1', bob: 'bob=2' });