`Type 'string' cannot be used to index type 'T'` when indexing a generic function parameter
See original GitHub issueBug 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:
- It should work, as the getter already works
- Or at least the error message should be changed as it’s misleading, indexing itself has no problem.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:9
- Comments:20 (8 by maintainers)
Top Results From Across the Web
Type '"test"' cannot be used to index type 'T' - Stack Overflow
Note that for type inference to work, you'll need to pass a parameter to the function that uses one or more of the...
Read more >Type 'String' cannot be used as an index type in TypeScript
The "Type 'String' cannot be used as an index type" TypeScript error occurs when we use the String type instead of string (lowercase...
Read more >Type 'string' cannot be used to index type 'T' while T extends ...
I wrote the following rename function: const rename = <T extends Record<string, any>>(object: T, namesMap: Record<string, string>) ...
Read more >How To Let a TypeScript Function Accept an Index Type as ...
You will get an error like “Element implicitly has an 'any' type because expression of type 'string' can't be used to index type...
Read more >element implicitly has an 'any' type because ... - You.com
'...expression of type string cannot be used to index...' ... You need to further constrain your generic type parameter: function ReturnFirst<Type extends ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The type of the property
a
is"hello"
, not the value (well, the value is too). It’s a property that can only accept the string literal"hello"
, but it can not accept the string""
. It’s more common used with union types, e.g."a" | "b"
. Meaning it can only be the string"a"
, or the string"b"
, but not any other string.@Tinadim
T
is a type parameter, which means that it can be anything that’s assignable to its constraint (i.e.extends
is not necessarily a subtyping relationship). This is maybe a bit counterintuitive, but being constrained by a type with an index signature doesn’t guarantee thatT
also has one:test(x)
is legal, butx['test'] = 1234;
is not. Therefore, the assignment insidetest
is also invalid. When you write a generic function, it’s not just the value that varies, but the type as well, so the compiler wants to ensure that what you write inside the function is valid for all possible typesT
can be.