Skip to content

Fix inferFromUsage on index signatures #25934

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

Merged
merged 2 commits into from
Jul 25, 2018

Conversation

sandersn
Copy link
Member

  1. Check the argumentExpression to determine the index signature type.
    Previously, the code mistakenly checked the type of the element access
    itself, which never returned a good type.
  2. If inference for the index signature type fails, substitute anyType.
    Previously, the code would create a bad index signature with an
    undefined type.
  3. Add tests. Previously, there were no tests of inferring from index signature usage.
  • Fixing (1) means that number index signatures are now created.
    Previously, only string index signatures would be created.
  • Fixing (2) means that index signatures will be inferred from single
    usage like return a[0]. Previously, the refactoring would fail,
    perhaps because of a check when stringifying the index signature (I
    haven't tracked down why.)

sandersn added 2 commits July 25, 2018 08:49
1. Check the argumentExpression to determine the index signature type.
Previously, the code mistakenly checked the type of the element access
itself, which never returned a good type.
2. If inference for the index signature type fails, substitute anyType.
Previously, the code would create a bad index signature with an
undefined type.
3. Add tests. Previously, there were no tests.

* Fixing (1) means that number index signatures are now created.
Previously, only string index signatures would be created.
* Fixing (2) means that index signatures will be inferred from single
usage like `return a[0]`. Previously, the refactoring would fail,
perhaps because of a check when stringifying the index signature (I
haven't tracked down why.)
@sandersn sandersn requested review from a user and mhegazy July 25, 2018 16:19
@sandersn sandersn merged commit 9658b47 into master Jul 25, 2018
@sandersn sandersn deleted the fix-infer-from-usage-index-signature branch July 25, 2018 17:51
//// return a[0] + 1;
////}

verify.rangeAfterCodeFix("a: { [x: number]: number; }",/*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, 0);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we considered just inferring Array? Seems like that would be accurate 99% of the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants