You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Missing error when mistyped value overwrites property of spreaded generic type parameter #42690
interfaceInputProps{value: string;}constgetProps=(props: InputProps): InputProps=>({
...props,value: 123,// when there is no generic type, it shows an error, as expected});constgetPropsGenericNoSpead=<TextendsInputProps>(props: T): T=>{constnewProps={...props};newProps.value=123;// also error, as expectedreturnnewProps;};constgetPropsGeneric=<TextendsInputProps>(props: T): T=>({
...props,value: 123,// no error here});
changed the title [-]False-positive when using object spread with a generic type[/-][+]Missing error when mistyped value overwrites property of spreaded generic type parameter[/+]on Feb 8, 2021
In situations where generics are not used, the use of "Object.assign" may be a solution.
functiontest(param: {[key: string]: (string|number)}){// Type '{ foo: string; } & { [key: string]: string | number; }' is not assignable to type '{ [key: string]: string; }'.// 'string' index signatures are incompatible.// Type 'string | number' is not assignable to type 'string'.// Type 'number' is not assignable to type 'string'.(2322)conststringValues: {[key: string]: string}=Object.assign({},{foo: 'bar'},param);console.log(stringValues);}
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.
functionfoo<Textendsstring>(keys: T[]): Record<T,number>{returnkeys.reduce((acc,key)=>{return{
...acc,[key]: 'string'// this is not a number, but there is no error}},{}asRecord<T,number>);}
Activity
[-]False-positive when using object spread with a generic type[/-][+]Missing error when mistyped value overwrites property of spreaded generic type parameter[/+]misuken-now commentedon Mar 15, 2021
I often run into the same problem and have trouble with it.
Patterns encountered in React.
playground
The cause seems to be related to the intersection type and
never, as shown below.playground
{ id: string; } & { id: number; }will evaluate to{ id: never }.playground
conditional types
extendsare allfalse, so{ id: number }cannot be assigned to{ id: string }.playground
If there was a check for assignability before processing with the intersection type, it would work as expected, wouldn't it?
misuken-now commentedon Mar 15, 2021
I was able to write a function to check the problem.
playground
vkarponen commentedon Oct 4, 2021
It would seem that the problem appears also without generics:
is falsely not considered an error but if you remove
foo: 'bar',from the above then TS considers that to be an error as it should.misuken-now commentedon Oct 7, 2021
In situations where generics are not used, the use of "Object.assign" may be a solution.
playground
teynar commentedon Nov 14, 2022
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
kumikumi commentedon Aug 23, 2023
This is probably related to the same issue:
Playground Link