Skip to content

Type 'string' cannot be used to index type 'T' when indexing a generic function parameterΒ #47357

Closed
@saschanaz

Description

@saschanaz

Bug Report

πŸ”Ž Search Terms

Type 'string' cannot be used to index type 'T'.

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about index signature

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function foo<T extends Record<string | symbol, any>>(target: T, p: string | symbol) {
  target[p]; // This is okay
  target[p] = ""; // This errors
}

πŸ™ Actual behavior

(parameter) target: T extends Record<string | symbol, any>
Type 'string' cannot be used to index type 'T'.(2536)
Type 'symbol' cannot be used to index type 'T'.(2536)

πŸ™‚ Expected behavior

Either:

  1. It should work, as the getter already works
  2. Or at least the error message should be changed as it's misleading, indexing itself has no problem.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions