microsoft / microsoft/TypeScript

Missing error when mistyped value overwrites property of spreaded generic type parameter

Open
#42,690 6 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Type Inference
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Bug Report

🔎 Search Terms

generic argument, rest spread, false positive

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about object spread and generics

I found a few relevant issues, but none of them fully correspond to the current one:

⏯ Playground Link

Playground link with relevant code

💻 Code
interface InputProps {
  value: string;
}

const getProps = (props: InputProps): InputProps => ({
  ...props,
  value: 123, // when there is no generic type, it shows an error, as expected
});

const getPropsGenericNoSpead = <T extends InputProps>(props: T): T => {
  const newProps = {...props};
  newProps.value = 123; // also error, as expected
  return newProps;
};


const getPropsGeneric = <T extends InputProps>(props: T): T => ({
  ...props,
  value: 123, // no error here
});
🙁 Actual behavior

Typescript allows assigning incompatible types

🙂 Expected behavior

Typescript shows an error

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 Playground example and compare the generic spread case with the non-generic and non-spread cases shown in the report. Trace the type-checking behavior for the value: 123 assignment in the generic object spread, and consider the issue done when TypeScript reports the expected incompatibility without regressing the other examples.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.