Skip to content

Conversation

@mhughes2012
Copy link

Fixes #30408

Description

This PR addresses issue #30408 where indexing an object with a string literal or a generic type parameter constrained to string would fail to fall back to an available string index signature if a specific property was not found. This often resulted in any types or confusing "property missing" errors even when a valid index signature existed.

The change modifies the property lookup logic in src/compiler/checker.ts to:

  1. First, check for a specific named property (preserving existing priority).
  2. Second, if no specific property is found, check if the index type is assignable to string.
  3. If assignable, attempt to resolve the type via the string index signature.

Checklist

  • There is an associated issue in the Backlog milestone
  • Code is up-to-date with the main branch
  • You've successfully run hereby runtests locally
  • There are new or updated unit tests validating the change

Test Case

I have added a new compiler test case tests/cases/compiler/stringLiteralIndexingWithIndexSignature.ts which validates:

  • Literal indexing on a type with a string index signature.
  • Generic indexing (K extends string) correctly resolving to the index signature's type.
  • Verification that reference baselines correctly capture these inferred types.

…nd generic indexing

Ensure that string literal indexing and type parameters constrained to string
correctly fall back to an available string index signature if a specific
property is not found. This prevents confusing 'any' or 'missing property'
errors when a valid indexer exists.
@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Dec 24, 2025
@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Dec 24, 2025
@mhughes2012
Copy link
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

Confusing error message for labels used before definition

2 participants