Quantcast
Channel: User Simon_Weaver - Stack Overflow
Viewing all articles
Browse latest Browse all 116

Answer by Simon_Weaver for How do you use spread operator to overwrite properties in typescript

$
0
0

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 :-)


Viewing all articles
Browse latest Browse all 116

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>