microsoft / microsoft/TypeScript

Object spreading produces wrong type with non-literal keys

Open
#56,431 12 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Index Types Experimentation Needed
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground reproduction and verify the reported assignment is accepted. Trace how object spread with a non-literal string index key is typed, then confirm the fix rejects the number-valued spread in this example and add or update coverage for the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.