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

Object spreading produces wrong type with non-literal keys #56431

Open
coolCucumber-cat opened this issue Nov 16, 2023 · 6 comments Β· May be fixed by #56441
Open

Object spreading produces wrong type with non-literal keys #56431

coolCucumber-cat opened this issue Nov 16, 2023 · 6 comments Β· May be fixed by #56441
Labels
Bug A bug in TypeScript Experimentation Needed Someone needs to try this out to see what happens
Milestone

Comments

@coolCucumber-cat
Copy link

πŸ”Ž Search Terms

object spread, object rest, object spreading

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about object spreading

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.2.2#code/CYUwxgNghgTiAEYD2A7AzgF0QLngb3gG0BrEAT10xgEsUBzAXVxQFcBbAIxBngF8BYAFDJ0WKLgIlylDDXpN4VWnT7wAvPiEBIcfADkegDTaAdGbBDeQA

πŸ’» Code

declare const c: { [key: string]: number }
const a: { [key: string]: string } = {
	a: '',
	...c
}

πŸ™ 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 case string, which could be any string, it doesn't care what's in it.

@fatcerberus
Copy link

Just to make the issue more concrete...

TS Playground

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.

@MartinJohns
Copy link
Contributor

I have a feeling that this is due to #27273.

@coolCucumber-cat
Copy link
Author

@MartinJohns Yeah that's probably it. It's been 5 years, I wonder why it's still open.

@MartinJohns
Copy link
Contributor

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.

@coolCucumber-cat
Copy link
Author

@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.

@andrewbranch andrewbranch added Bug A bug in TypeScript Experimentation Needed Someone needs to try this out to see what happens labels Nov 16, 2023
@andrewbranch
Copy link
Member

Talked briefly about this with @ahejlsberg and decided to revisit making the resulting object type { [key: string]: string | number }β€”that is, if you spread in an index signature, the resulting type should have an index signature whose value type is the union of all property types and index signature types seen in the target objectβ€”and see what breaks in the extended test suites.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Experimentation Needed Someone needs to try this out to see what happens
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants