ts-data-forge
    Preparing search index...

    Function fromEntries

    • Creates an object from an array of key-value pairs with precise TypeScript typing. This is a type-safe wrapper around Object.fromEntries that provides better type inference and compile-time guarantees about the resulting object structure.

      Type Behavior:

      • When entries is a fixed-length tuple, the exact object type is inferred
      • When entries has dynamic length with union key types, Partial is applied to prevent incorrect assumptions about which keys will be present

      Type Parameters

      • const Entries extends readonly (readonly [PropertyKey, unknown])[]

        The readonly array type of key-value entry tuples

      Parameters

      • entries: Entries

        An array of readonly key-value entry tuples [key, value]

      Returns IsFixedLengthList<Entries> extends true
          ? Readonly<EntriesToObjectImpl<{}, Entries>>
          : PartialIfKeyIsUnion<
              TypeEq<Entries["length"], 0> extends true
                  ? never
                  : TypeEq<Tail<Entries>["length"], 0> extends true
                      ? Entries[0][0]
                      : TypeEq<Tail<Tail<Entries>>["length"], 0> extends true
                          ? Entries[0][0] | Tail<Entries>[0][0]
                          : TypeEq<Tail<Tail<Tail<(...)>>>["length"], 0> extends true
                              ? Entries[0][0] | Tail<Entries>[0][0] | Tail<Tail<(...)>>[0][0]
                              : TypeEq<Tail<Tail<(...)>>["length"], 0> extends true
                                  ?
                                      | Entries[0][0]
                                      | Tail<(...)>[0][0]
                                      | Tail<(...)>[0][0]
                                      | Tail<(...)>[0][0]
                                  : TypeEq<Tail<(...)>["length"], 0> extends true
                                      ?
                                          | (...)[(...)][0]
                                          | (...)[(...)][0]
                                          | (...)[(...)][0]
                                          | (...)[(...)][0]
                                          | (...)[(...)][0]
                                      : TypeEq<(...)[(...)], 0> extends true
                                          ?
                                              | (...)[(...)]
                                              | (...)[(...)]
                                              | (...)[(...)]
                                              | (...)[(...)]
                                              | (...)[(...)]
                                              | (...)[(...)]
                                          : TypeEq<(...), (...)> extends true
                                              ? (...) | (...) | (...) | (...) | (...) | (...) | (...)
                                              : (...) extends (...) ? (...) : (...),
              ReadonlyRecord<
                  TypeEq<Entries["length"], 0> extends true
                      ? never
                      : TypeEq<Tail<Entries>["length"], 0> extends true
                          ? Entries[0][0]
                          : TypeEq<Tail<Tail<Entries>>["length"], 0> extends true
                              ? Entries[0][0] | Tail<Entries>[0][0]
                              : TypeEq<Tail<Tail<(...)>>["length"], 0> extends true
                                  ? Entries[0][0] | Tail<(...)>[0][0] | Tail<(...)>[0][0]
                                  : TypeEq<Tail<(...)>["length"], 0> extends true
                                      ?
                                          | (...)[(...)][0]
                                          | (...)[(...)][0]
                                          | (...)[(...)][0]
                                          | (...)[(...)][0]
                                      : TypeEq<(...)[(...)], 0> extends true
                                          ?
                                              | (...)[(...)]
                                              | (...)[(...)]
                                              | (...)[(...)]
                                              | (...)[(...)]
                                              | (...)[(...)]
                                          : TypeEq<(...), (...)> extends true
                                              ? (...) | (...) | (...) | (...) | (...) | (...)
                                              : (...) extends (...) ? (...) : (...),
                  TypeEq<Entries["length"], 0> extends true
                      ? never
                      : TypeEq<Tail<Entries>["length"], 0> extends true
                          ? Entries[0][1]
                          : TypeEq<Tail<Tail<Entries>>["length"], 0> extends true
                              ? Entries[0][1] | Tail<Entries>[0][1]
                              : TypeEq<Tail<Tail<(...)>>["length"], 0> extends true
                                  ? Entries[0][1] | Tail<(...)>[0][1] | Tail<(...)>[0][1]
                                  : TypeEq<Tail<(...)>["length"], 0> extends true
                                      ?
                                          | (...)[(...)][1]
                                          | (...)[(...)][1]
                                          | (...)[(...)][1]
                                          | (...)[(...)][1]
                                      : TypeEq<(...)[(...)], 0> extends true
                                          ?
                                              | (...)[(...)]
                                              | (...)[(...)]
                                              | (...)[(...)]
                                              | (...)[(...)]
                                              | (...)[(...)]
                                          : TypeEq<(...), (...)> extends true
                                              ? (...) | (...) | (...) | (...) | (...) | (...)
                                              : (...) extends (...) ? (...) : (...),
              >,
          >

      An object created from the entries with precise typing

      // Fixed-length tuple - exact type inferred
      const entries1 = [
      ['name', 'David'],
      ['age', 25],
      ['active', true],
      ] as const;

      const obj1 = Obj.fromEntries(entries1);

      assert.deepStrictEqual(obj1, {
      name: 'David',
      age: 25,
      active: true,
      });

      // Dynamic length array - Partial type applied
      const dynamicEntries: (readonly ['x' | 'y', number])[] = [
      ['x', 10],
      ['y', 20],
      ];

      const obj2 = Obj.fromEntries(dynamicEntries);

      assert.deepStrictEqual(obj2, { x: 10, y: 20 });