Returns the entries of an object of type T. This is like Object.entries except the return type
captures the known keys and value types of T.
Note that it is the responsibility of the caller to use this wisely -- there are cases where
the runtime set of entries returned may be broader than the type checked set at compile time,
so there's potential for this to be abused in ways that are not inherently type safe. For
example, given base class Animal, subclass Fish, and const animal: Animal = new Fish();
then entriesOf(animal) will not type-check the entire set of keys of the object animal since
it is actually an instance of type Fish, which has an extended property set.
In general, it should be both convenient and type-safe to use this when enumerating the entries
of simple data objects with known properties.
interfacePoint { x: number; y: number; } constpoint: Point = { x:1, y:2 }; // type of entries -> ['x' | 'y', number][] constentries = entriesOf(point); for (constentryofentries) { console.log(entry[0], entry[1]); } // x 1 // y 2
Returns the entries of an object of type
T
. This is likeObject.entries
except the return type captures the known keys and value types ofT
.Note that it is the responsibility of the caller to use this wisely -- there are cases where the runtime set of entries returned may be broader than the type checked set at compile time, so there's potential for this to be abused in ways that are not inherently type safe. For example, given base class
Animal
, subclassFish
, andconst animal: Animal = new Fish();
thenentriesOf(animal)
will not type-check the entire set of keys of the objectanimal
since it is actually an instance of typeFish
, which has an extended property set.In general, it should be both convenient and type-safe to use this when enumerating the entries of simple data objects with known properties.