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

How to get object string index name? #1572

Open
kemper0110 opened this issue Sep 6, 2024 · 0 comments
Open

How to get object string index name? #1572

kemper0110 opened this issue Sep 6, 2024 · 0 comments

Comments

@kemper0110
Copy link

Is your feature request related to a problem? Please describe.

Firstly, devcontainer to test a problem.
https://codesandbox.io/p/sandbox/wpkrx7

I struggled how to get name of string index of object type.
For example, for this type I want to extract projectId identifier.

type ProjectJobTypePeriodVal = {
    [projectId: string]: { 
        name: string
        duration: string
    }
}

By debugging and search in sources I came to this solution

const indexType = type.getStringIndexType(); // expect this not undefined

const keyName = type.compilerType.checker
  .getIndexInfosOfType(type.compilerType)[0]
  .declaration.locals.keys()
  .next().value;
// finally keyname is `projectId`

Describe the solution you'd like

I am expecting my solution is bad. Maybe I missed a proper function or approach?

Describe alternatives you've considered

My alternative described above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant