镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Sometimes, keyof any is string #61203

Description

🔎 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

Activity

  1. mkantor commented on Feb 21, 2025

    @mkantor
    Contributor

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

  2. RyanCavanaugh commented on Feb 24, 2025

    @RyanCavanaugh
    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

  3. AFatNiBBa commented on Feb 25, 2025

    @AFatNiBBa
    Author

    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?

  4. typescript-bot commented on Feb 28, 2025

    @typescript-bot
    Contributor

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

  5. AFatNiBBa commented on Mar 6, 2025

    @AFatNiBBa
    Author

    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

  6. AFatNiBBa commented on Mar 6, 2025

    @AFatNiBBa
    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)
  7. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions