Object.entries({ a: 42 }) returns [string, unknown][]
matzkoh opened this issue · comments
In the typescript lib, the union type of values simply becomes the new value type.
entries<T>(o: { [s: string]: T } | ArrayLike<T>): [string, T][];
Compared to the above, the implementation of this lib is a bit more complex.
Please let me know if I've missed something.
Thank you for reporting this, and sorry for late response.
This is actually an “improvement” compared to the original result; see the below example.
better-typescript-lib considers the possibility of existence of properties that do not appear in types.
Actually TypeScript's lib does the same thing for Object.keys
(which always returns string[]
), but somehow doesn't for Object.entries
and Object.values
.
Ideally we should have detected an object literal directly passed to Object.entries
so that such a consideration isn't needed, but I think this isn't possible with current type system.
type FooObj = {
foo: number;
}
const obj1 = {
foo: 123,
bar: "Hello!"
}
const obj2: FooObj = obj1;
for (const [key, value] of Object.entries(obj2)) {
// Default TypeScript definition: value is number
// better-typescript-lib: value is unknown
console.log(value); // 123 and "hello!" are shown
}