Skip to content

Object.entries({ a: 42 }) returns [string, unknown][] #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
matzkoh opened this issue Jan 22, 2021 · 1 comment
Closed

Object.entries({ a: 42 }) returns [string, unknown][] #2

matzkoh opened this issue Jan 22, 2021 · 1 comment
Labels
design limitation Not ideal, but cannot be fixed

Comments

@matzkoh
Copy link

matzkoh commented Jan 22, 2021

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.

@uhyo uhyo added the design limitation Not ideal, but cannot be fixed label Mar 27, 2021
@uhyo
Copy link
Owner

uhyo commented Mar 27, 2021

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design limitation Not ideal, but cannot be fixed
Projects
None yet
Development

No branches or pull requests

2 participants