-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Object spreading produces wrong type with non-literal keys #56431
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
Just to make the issue more concrete... const b: { [key: string]: number } = {
b: 777
}
const a: { [key: string]: string } = {
a: "foo",
...b
}
console.log(a); prints: [LOG]: {
"a": "foo",
"b": 777
} which is a clear type violation. |
I have a feeling that this is due to #27273. |
@MartinJohns Yeah that's probably it. It's been 5 years, I wonder why it's still open. |
Priorities. There are more important things to work on than some rarely reported bugs involving fairly unsound types, and the manpower is limited. |
@MartinJohns It's pretty damn common and it's very obvious incorrect. There are some way more useless things that are being worked on and it's also been 5 years. |
Talked briefly about this with @ahejlsberg and decided to revisit making the resulting object type |
same here const a: Record<string, number> = { a: 1 }
new URLSearchParams({
x: '1',
...a // no error, but URLSearchParams requires Record<string, string>
}) |
A full list of different cases, if this helps: Playground |
๐ Search Terms
object spread, object rest, object spreading
๐ Version & Regression Information
โฏ Playground Link
https://www.typescriptlang.org/play?ts=5.2.2#code/CYUwxgNghgTiAEYD2A7AzgF0QLngb3gG0BrEAT10xgEsUBzAXVxQFcBbAIxBngF8BYAFDJ0WKLgIlylDDXpN4VWnT7wAvPiEBIcfADkegDTaAdGbBDeQA
๐ป Code
๐ Actual behavior
It allows numbers to be in the object, when they shouldn't be allowed.
๐ Expected behavior
It shouldn't allow numbers.
Additional information about the issue
Because
c
has non-literal keys, in this casestring
, which could be any string, it doesn't care what's in it.The text was updated successfully, but these errors were encountered: