0

i have a function to format the date object (especially zip code (converting from 12345-678 to 12345)), because i need to compare 2 address objects(for ex: physical address and mailing address ).

 formatAddress(address: any) {
    const { country , addressType , ...addressCopy } = address;
    const { postalCode } = addressCopy;
    const formattedPostalCode = postalCode && postalCode.split('-')[0];
    addressCopy.postalCode = formattedPostalCode;
    return addressCopy;
  }

i am destructing the address and removing the country and addressType because mailing address object has the extra country , addressTypeto compare with physical address.

but when i pass the phyical address to formatAddress function , it is failing because it doesn't have country , addressType , how to fix this and can we assign undefined to country , addressType if the address doesn't have country , addressType keys.

sravan ganji
  • 4,774
  • 3
  • 25
  • 37
  • Please provide a [mre] that clearly demonstrates the issue you are facing. Ideally someone could paste the code into a standalone IDE like [The TypeScript Playground (link here!)](https://tsplay.dev/Wy4b6m) and immediately get to work solving the problem without first needing to re-create it. So there should be no pseudocode, typos, unrelated errors, or undeclared types or values. – jcalz Apr 06 '22 at 15:24
  • 1
    Extracting non-existing properties works just fine out of the box: `var {foo} = {}; console.log(foo)`. Whatever issues you have it's not because the object doesn't have those properties (or at least that would not cause the destructuring to fail). Please provide more information about what actually fails. Also you may want to start by using a better type than `any` to get help from Typescript. – Felix Kling Apr 06 '22 at 15:26

0 Answers0