Skip to content

Incorrect type declaration emitted for object with numeric string keys #55292

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

Closed
kazushisan opened this issue Aug 7, 2023 · 0 comments Β· Fixed by #55298
Closed

Incorrect type declaration emitted for object with numeric string keys #55292

kazushisan opened this issue Aug 7, 2023 · 0 comments Β· Fixed by #55298
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@kazushisan
Copy link

πŸ”Ž Search Terms

numeric string key object

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ

⏯ Playground Link

https://www.typescriptlang.org/play?#code/MYewdgzgLgBAygFQIIIKpxgXhgbwFAyEwDaARACwAM5pAugFwylgiwBmIArmACal4BfGAEMIMUJCgBuPHigBPAA4BTeFGFROY7AGtl8kGxgKVh+MjRwgA

πŸ’» Code

const STATUS = {
    ["404"]: "not found"
} as const;

type Status = keyof typeof STATUS;

πŸ™ Actual behavior

the type of Status will be the following.

type Status = "404"

however when I create a .d.ts file, the type of Status will not be the same. This is caused by the incorrect declare const in the declaration file that changes the numeric string keys to a number.

type Status = 404

πŸ™‚ Expected behavior

Given the example source code, the type for Status when referring to the declaration file will be the same as in the source code.

type Status = "404"

Note: this issue does not reproduce if the numeric string key used in the object is not a variable.

const STATUS = {
    "404": "not found"
} as const;

type Status = keyof typeof STATUS;
// type Status = "404"
// the result will be the same for both the source file and the type declaration file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants