-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Sometimes, keyof any
is string
#61203
Comments
I came across a relevant comment that may be of interest to anyone looking at this issue. |
Seems 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 |
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? |
This 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. |
If 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) |
π 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
Obj
type allows indexing only with stringsπ Expected behavior
The
Obj
type should allow indexing with every type of property keyAdditional information about the issue
No response
The text was updated successfully, but these errors were encountered: