TypeScript: Incorrect "'value' is specified more than once, so this usage will be overwritten. ts(2783)"
TypeScript Version: 3.9.2
Search Terms:
- property-will-be-overwritten-by-spread
- 2783
- is specified more than once, so this usage will be overwritten
- spread
Code
import { Select } from '@material-ui/core';
import React from 'react';
export function Repro({ SelectProps = {} }: { SelectProps?: Partial<React.ComponentProps<typeof Select>> }) {
return (
<Select value={'test'} {...SelectProps} />
);
}
Expected behavior:
No error, as value in SelectProps is optional and SelectProps even defaults to an empty object.
Actual behavior:
‘value’ is specified more than once, so this usage will be overwritten.ts(2783) Repro.tsx(7, 32): This spread always overwrites this property.
Playground Link: none, as the playground does not seem to load the material-ui types
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 23
- Comments: 36 (4 by maintainers)
Commits related to this issue
- Using typescript version 3.8.3 The build was failing due to the typescript version `3.9.2` has a bug https://github.com/microsoft/TypeScript/issues/38535 We will use the version prevous `3.9.2` whic... — committed to CartoDB/toolkit by manmorjim 4 years ago
- Pin all pacakges to typescript 3.8.3 The build was failing due to the typescript version `3.9.2` has a bug microsoft/TypeScript#38535 We will use the version prevous `3.9.2` which is `3.8.3` — committed to CartoDB/toolkit by manmorjim 4 years ago
- Upgrade to TypeScript 3.9.3 I was hitting a bug in 3.9.2 with the breadcrumbs CL [1] that is fixed in 3.9.3 via this PR [2]. (I've verified locally that it is fixed). [1]: https://github.com/microso... — committed to ChromeDevTools/devtools-frontend by jackfranklin 4 years ago
- Upgrade to TypeScript 3.9.3 I was hitting a bug in 3.9.2 with the breadcrumbs CL [1] that is fixed in 3.9.3 via this PR [2]. (I've verified locally that it is fixed). [1]: https://github.com/microso... — committed to binaryage/dirac by jackfranklin 4 years ago
- Use Object.assign to prevent TypeScript error The use of the spread opertor causes issues in TypeScript 3.9.x To avoid this issue we use Object.assign to return on object with default values. TypeSc... — committed to bumi/joule-extension by bumi 4 years ago
I noticed that this issue has 4.0 as milestone. If 4.0 will have breaking changes and this bug was introduced in 3.9.2, it might be worth to consider making a fix for this bug in 3.9.x?
@DanielRosenwasser 3.9.3 not work
FWIW, for people like me who find themselves on this thread, this problem is resolved in React by including the prop after a spread:
// Error:
// No error:
Same problem with 3.9.6
@yogeshkotadiya without knowing the type of
payload, that message can be perfectly valid. Does the type ofpayloadcontain a non-optionaladdressNickNameproperty? Then the message is correct and telling you thataddressNickName: "Home"will always be overridden bypayload.addressNickNameso you can leave it out. In that case, it is not this bug.I’m not sure what you want from me, but:
3.9.3, as indicated by the milestone and associated PR activity.3.9.3, so didn’t have the patch (his comment includes a reference to3.9.2)The issue is not resolved in 3.9.3
Should be fixed now
Same problem with
Reverting typescript to
"typescript": "3.8.3",solves the problem.@nisimjoseph If the object you are receiving does not contains all the mandatory fields, then it is not a
Configobject. The easiest solution I see to do what you want is changing your code toAnyway, this is not a bug: compiler is right.
I am using TS 3.9.6 and the issue still exist. see the simple code here:
Get error:
TS2783: 'publisher' is specified more than once, so this usage will be overwritten.When I do...<any>config,it pass, but I do want the static check which I can’t get now.Which has a different runtime behaviour and exactly what the error is warning about. In the first case, assuming the types were correct,
iconOnlywould always be overwritten and never actually have that value. You probably don’t intend that, which is why you get the error.@Talent-Rain
SelectPropsis a Partial property. This means every of it properties can be there, but it could also be not. It will not necessarily be overridden, but it could be. Which is fine, and a case where that error message should not have shown up.Could we please stop posting unrelated stuff here? This issue has been fixed and everything after that is either worthy of a new ticket or not of interest any more, as this bug has been fixed.
@phryneas nvm. I leave it here for the log: when I was trying to create a simple snippet to replicate the issue, I noticed it was compiling fine. So the solution jumped to my mind. The error resides in the
as AndroidVariantandas IOSVariantsince they both inherit fromVariantwhose definition is:So, even if I don’t really specify the
nametwo times, since it is defined asmandatoryin the base interface the compiler sees it as specified 2 times: my error, compiler was right.@yamuun it’s useless to write
cause age won’t be 20 in hoge. The spread always overwrites age property. So, it’s ok to see “This spread always overwrites this property”.