I had some defaults set like this that weren't a problem until Typescript 4.4 (possibly an earlier version):
Fails
const formsValue = { stateOrProvince: null, stateCd: null, address3: null, phone: null, ...addressFields };
Shows error: Error TS2783: 'stateOrProvince' is specified more than once, so this usage will be overwritten.
which makes sense.
The destination for this value is an Angular form which requires empty fields to be explicitly set. While normally addressFields
would contain a value for every field (as specified by its type) I had some older customers who may have a cached value in their local storage without all these fields hence these defaults.
Fortunately the following seems to get around the error, and I think looks nicer anyway as it groups things together.
Works
const formsValue = { ...{ stateOrProvince: null, stateCd: null, address3: null, phone: null, }, ...addressFields };
Not 100% sure why the compiler lets this through but it does :-)