Skip to content
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

Iterators: Narrowing the key does not narrow the value's type #46718

Closed
5 tasks done
MarcelWaldvogel opened this issue Nov 7, 2021 · 5 comments
Closed
5 tasks done

Iterators: Narrowing the key does not narrow the value's type #46718

MarcelWaldvogel opened this issue Nov 7, 2021 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@MarcelWaldvogel
Copy link

Suggestion

Narrowing the key obtained by iterating through Object.entries() does not narrow the type of the value associated with that key. I.e., inside for (const [key, value] of Object.entries(x)) {…}, x[key] can have its type narrowed by narrowing key, but the type of value will not be narrowed, even though the two are equivalent.

Right now, it feels somehow like a surprise that x[key] is not the same as value and x[key] needs to be evaluated again, even though that was the original goal of using the iterator.

🔍 Search Terms

object type narrowing key value

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

📃 Motivating Example

Example (Playground):

type X = {a: number, b: number, c: string};

function isKeyToNumber(s: string): s is 'a' | 'b' {
  return s === 'a' || s=== 'b';
}

export function doubleNumbers(x: X): void {
  for (const [k, v] of Object.entries(x)) {
    if (isKeyToNumber(k)) {
      const y = v * 2;    // v is not known to be a number ❌
      const z = x[k] * 2; // x[k] is known to be a number ✅
    }
  }
}

I ran into this issue while writing a sort function for a TypeScript-first vCard parser, which I mis-attributed first.

💻 Use Cases

Enumerating through objects which have different types for their values based on the key, e.g.

  • for databases
  • JSON inputs
  • vCards

Right now,

  • for…of cannot be reasonably used there, as the value's type is not narrowed, unless re-fetched from the record; and
  • the developer is confused why the same data has two different types, depending on how they try to get at it.
@MartinJohns
Copy link
Contributor

This sounds like a duplicate of #35873.

@MarcelWaldvogel
Copy link
Author

I do not think it is a duplicate, but they probably should be implemented together, even though the key/value case might be easier to implement.

@MartinJohns
Copy link
Contributor

I don't see how this could be implemented without a very specific implementation just for Object.entries(), and that would be silly and counterproductive. But I'd love to learn and be corrected.

@andrewbranch
Copy link
Member

It is a duplicate of #35873—this is just a specific example where it would be nice to have #35873.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Nov 8, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants