Skip to content
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

Closed
AFatNiBBa opened this issue Feb 17, 2025 · 6 comments Β· May be fixed by #61224
Closed

Sometimes, keyof any is string #61203

AFatNiBBa opened this issue Feb 17, 2025 · 6 comments Β· May be fixed by #61224
Labels
Not a Defect This behavior is one of several equally-correct options

Comments

@AFatNiBBa
Copy link

πŸ”Ž 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

type Obj = { [k in keyof any]: number }
//   ^? type Obj = { [x: string]: number; }

type Keys = keyof Obj;
//   ^? type Keys = string | number | symbol

declare const obj: Obj;
obj[Symbol.iterator] // (This breaks)

πŸ™ Actual behavior

The Obj type allows indexing only with strings

πŸ™‚ Expected behavior

The Obj type should allow indexing with every type of property key

Additional information about the issue

No response

@mkantor
Copy link
Contributor

mkantor commented Feb 21, 2025

I came across a relevant comment that may be of interest to anyone looking at this issue.

@RyanCavanaugh
Copy link
Member

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 type Obj = { [k: string | number | symbol]: number } instead

@RyanCavanaugh RyanCavanaugh added the Not a Defect This behavior is one of several equally-correct options label Feb 24, 2025
@AFatNiBBa
Copy link
Author

AFatNiBBa commented Feb 25, 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
Copy link
Collaborator

This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 28, 2025
@AFatNiBBa
Copy link
Author

AFatNiBBa commented Mar 6, 2025

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 wrong

@AFatNiBBa
Copy link
Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Defect This behavior is one of several equally-correct options
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants