-
Notifications
You must be signed in to change notification settings - Fork 12.8k
ES2019 Object.fromEntries uses PropertyKeys as mapped key type #31393
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
Which line are you expecting to fail? |
Sorry @RyanCavanaugh I didn't explain my test case correctly. Even with the desired behaviour I think |
@RyanCavanaugh Would you mind if I attempted this and raised a PR please? |
I believe this is also causing an error in our project. I have distilled the error down to the following simple repo which I think is the same issue @AWare is describing here. package.json:
tsconfig.good.json:
tsconfig.bad.json:
index.ts:
When I run The only difference in the "bad" build is that i've set "keyofStringsOnly": true instead of false. This is a stripped down version of my actual configuration but I believe it is just the interaction of "keyofStringsOnly":true and lib:["esnext"] that is relevant to this issue. |
Having same issue on my side, same configuration ( |
Got the same issue randomly, not sure what triggered it as things were compiling fine. But using a target of either Replication details are as follows, I did this in my tsconfig.json {
"compilerOptions": {
"target": "es2019",
"keyofStringsOnly": true,
},
} Running
This is specifically part of {
"compilerOptions": {
"target": "es2018",
"keyofStringsOnly": true,
},
} |
I changed node_modules/typescript/lib/lib.es2019.object.d.ts:28:81 - error TS2322: Type 'string | number | symbol' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.
28 fromEntries<T = any>(entries: Iterable<readonly [PropertyKey, T]>): { [k in PropertyKey]: T }; Here is my {
"compilerOptions": {
"target": "es2018",
"moduleResolution": "node",
"module": "commonjs",
"sourceMap": true,
"rootDir": "src",
"outDir": "dist",
"keyofStringsOnly": true,
"lib": ["esnext", "dom"],
"esModuleInterop": true,
},
"exclude": ["node_modules", "**/*.spec.ts"]
} The only thing that works is removing I am not sure why this is still happening. Has anyone else experienced this/ any suggestions on a fix? |
It sounds like there are really two bugs here: the more serious is that the current definition of I see 3 possible fixes for these two bugs:
Because this isn't a 3.8 regression, I'm not going to fix this in the 3.8 beta period — any change to the DOM types is going to break someone. I'll look at this when 3.9 starts. |
Is there a particularly good workaround for this in the meantime without setting *edit - for now I have set |
My question is this: I want
Is there a motivation for why a more precise typing like that would be unsafe at the library level? I use enums a lot as keys and I'd like for mapping across them to preserve the fact that the keys are from my enum, not just a string or maybe a number or something. |
PropertyKey is accurate but not usable. Fixes #31393
I opened #37457 which makes the return type |
PropertyKey is accurate but not usable. Fixes #31393
Uh oh!
There was an error while loading. Please reload this page.
TypeScript Version: 3.5.0-dev.20190514
Search Terms:
fromEntries
Code
Expected behavior:
Compilation should fail becausenumber
is not a key ofa
.edit: The return type of
Object.fromEntries(b)
should be inferred asd
:{ [key: string]: T }
.Actual behavior:
edit: The return type of
Object.fromEntries(b)
is{[key: number]: T; [key: string]: T }
Adding an erroneous[key: number]
.Object.fromEntries
has type:However if I make a second type parameter for fromEntries which extends PropertyKey and use that for the key type in forEntries:
I'm willing to make a PR for this if this sounds like a reasonable approach.
Playground Link:
Apologies, but I could not use
es2019
in the playground.Related Issues:
#30934
#30933 #25999
The text was updated successfully, but these errors were encountered: