Missing error when mistyped value overwrites property of spreaded generic type parameter
See original GitHub issueBug 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:
- https://github.com/microsoft/TypeScript/issues/35858
- https://github.com/microsoft/TypeScript/issues/38469
- https://github.com/microsoft/TypeScript/issues/30129
⏯ 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
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:5
Top Results From Across the Web
Why does TypeScript complaint about missing property in ...
It complaints that F is not assignable to type Bar , and I don't understand why. ... I'd say you've misplaced the generic...
Read more >TypeScript errors and how to fix them
error TS1337 : An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead....
Read more >Linter rules - Dart
Avoid types as parameter names. This rule is available as of Dart 2.0.0. Rule sets: core, recommended, flutter. This rule has a quick...
Read more >C++ Core Guidelines - GitHub Pages
The C++ Core Guidelines are a set of tried-and-true guidelines, rules, and best practices about coding in C++.
Read more >Control flow and error handling - JavaScript - MDN Web Docs
catch statements. throw statement; try...catch statement. Exception types. Just about any object can ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I am also experiencing this issue and I feel quite surprised since this looks like a fairly common case when using reduce with the spread operator.
TypeScript Playground
In situations where generics are not used, the use of “Object.assign” may be a solution.
playground