Repository navigation
Sometimes, keyof any is string #61203
Description
Activity
I came across a relevant comment that may be of interest to anyone looking at this issue.
Reacted by Sean AlunniRyanCavanaugh commented
on Feb 24, 2025 MemberMore actionsSeems dangerous to change this, based on the PR results, and it's not super clear what the upside is. You can (and probably should?) write
type Obj = { [k: string | number | symbol]: number }instead- addedNot a DefectThis behavior is one of several equally-correct optionsThis behavior is one of several equally-correct options
on Feb 24, 2025 Here's something more similiar to what's actually happening to me
type A<T> = { [k in keyof T]: 1 }; declare function iDontKnow<T>(a: T): [ A<T>, keyof T ]; function wrap<T>(a: T) { const [ o, k ] = iDontKnow(a); return o[k]; } type a = { a: number, b: string }; const b: a = { a: 1, b: "ciao" }; const [ o1, k1 ] = iDontKnow({ a: 1, b: 2 }); const v1 = o1[k1]; // Ok: The type argument is inferred const [ o2, k2 ] = iDontKnow<{ a: number, b: number }>({ a: 1, b: 2 }); const v2 = o2[k2]; // Ok: The type argument is passed explicitly const v3 = wrap<any>({ a: 1, b: 2 }); // Ok: The type argument is passed through a generic parameter const [ o4, k4 ] = iDontKnow<any>({ a: 1, b: 2 }); const v4 = o4[k4]; // ↑ Type 'symbol' cannot be used as an index type. (TS2538)
Isn't it just wrong?
typescript-bot commented
on Feb 28, 2025 ContributorMore actionsThis issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes.
I'd like to get a response on this issue, since it has been closed without providing any.
I'm sorry, but I really don't get why this is "Not a Defect": The same operation, with the same arguments can give two different results and one of them is wrongIf the problem is still not clear, here's a another example
type A<T> = { [k in keyof T]: 1 }; declare function iDontKnow<T>(a: T): [ A<T>, keyof T ]; const something: any = { a: 1, b: 2 }; const [ o, k ] = iDontKnow(something); const v = o[k]; // // ↑ Type 'symbol' cannot be used as an index type. (TS2538)
- locked as resolved and limited conversation to collaborators
on Oct 22, 2025
🔎 Search Terms
keyof any string
🕗 Version & Regression Information
Tested in the playground on "v3.3.3333" and "Nightly", it occurs in both versions
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.8.0-dev.20250217#code/FAFwngDgpgBA8gIwFYwLwwN4wNoGsYCWAdjLlGAPYBmMAhkWALoBcMRArgLYJQBOMAX2AB6YTHEA9APzBQkWAGlyAZzSly1eMgDcIsZJnAAJlADGAG1q9YpikWUgYFZK0RJdzpNgDKYbhXMAOgIQPloQCl5GGFEYAAoAFQALAlUEa1pcZQBKIA
💻 Code
🙁 Actual behavior
The
Objtype allows indexing only with strings🙂 Expected behavior
The
Objtype should allow indexing with every type of property keyAdditional information about the issue
No response