-
Notifications
You must be signed in to change notification settings - Fork 12.8k
keyof { [x: string]: unknown }
unexpectedly includes number
#48269
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
Comments
This is working as intended: #23592 & Release Notes 2.9 |
If it is working as intended, should I'm assuming this description of
|
|
Keeping The inconsistency can't be eliminated, just moved around, since |
Leaving a breadcrumb for future readers: even though a single I assume this is #56985. const foo: Record<string, unknown> = {foo: 'foobar' };
const bar: Record<string, unknown> = {bar: 'foobar' };
const foobar = {...foo, ...bar };
const fooKey: keyof typeof foo = "whatever";
// ^? const fooKey: string;
const barKey: keyof typeof bar = "whatever";
// ^? const barKey: string;
const foobarKey: keyof typeof foobar = "whatever";
// ^? const foobarKey: string | number; |
Uh oh!
There was an error while loading. Please reload this page.
Bug Report
Types with an index signature for strings have
number
included inkeyof
, that is thatkeyof { [x: string]: unknown } = string | number
. I find this unexpected. A possible explanation for this behaviour is thatnumber
can index such types be being implicitly coerced to a string:However, consider that
Record<string, unknown>
is equally capable of such behaviour, andkeyof Record<string, unknown>
does not includenumber
:In that case, does it make sense for
number
to appear, considering it is not a prerequisite for numbers to implicitly index string-indexed types? Or alternatively, doesRecord<string, unknown>
incorrectly not reportnumber
?More examples are provided in the playground link.
🔎 Search Terms
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about
ctrl-f "keyof"
⏯ Playground Link
Playground link with relevant code
💻 Code
This is a short snippet of the playground example with the main inconsistent example.
🙁 Actual behavior
keyof { [x: string]: unknown }
unexpectedly includesnumber
, whereaskeyof Record<string, unknown>
does not, which is inconsistent.🙂 Expected behavior
keyof { [x: string]: unknown }
should not includenumber
.I find this more reasonable than the alternative possibly expected behaviour:
keyof Record<string, unknown>
and all types whichnumber
can index via being implicitly coerced to a string should includenumber
as a key.The text was updated successfully, but these errors were encountered: