1

I have something very strange happening... I have an array called destinations which takes objects with a dest, an amount and a memo.

My code is the following:

  • I have a variable "element" which (when logged to the console) is a string with value "xNUSrBmqocfTinhSZVV6vLtwzaWasCoYuTUqnERPYjd8CAj3TtAf3hTvsNzoeYRY5b4qFGBS4mZhpXiJDghMKMGuJeD4Qy74ZCe9oMreUuwcRKQVKjj55LUEbnSDPtzH71gyPuT1ft3"
  • But when I create an object and set the dest to this variable and push this object into the destinations array, it suddenly isn't the string above anymore but an object... (take a look at the attached screenshot to see the outputs of the console.logs)
const destinations: { dest: string | never[]; amount: number; memo: string; }[] = [];

console.log("ELEMENT")
console.log(element)
console.log("-----")
                      
destinations.push(
    {
        dest: element,
        amount: 1 * 1e8,
        memo: JSON.stringify(poll),
     }
) 

console.log("DESTINATIONS")
console.log(destinations)
console.log("-----")

enter image description here

Why is this happening?

Thanks in advance

xeraphim
  • 4,375
  • 9
  • 54
  • 102
  • At the time the object is pushed to `destinations` the `element` is an object – evolutionxbox Dec 07 '21 at 20:16
  • 3
    You should probably try to provide a [mre] that clearly demonstrates the issue you are facing. Ideally someone could drop the code into a standalone IDE like [The TypeScript Playground (link here!)](https://tsplay.dev/wj4j8m) and immediately get to work solving the problem without first needing to re-create it. There should be no typos, unrelated errors, or undeclared types or values. – jcalz Dec 07 '21 at 20:22
  • 1
    Some suggestions: `console.log` has been overridden in your environment, it displays strings for `Address` objects. The code you are showing is actually not synchronous but divided into asynchronous functions, there are reassignments of `element` between the `console.log`. As @jcalz said if you need more help about it you have to post a minimal reproducible example. – Guerric P Dec 07 '21 at 20:33
  • 1
    Try to log the output of `JSON.stringify` instead of the object directly. IIRC, when logging an object the browser shows you the object at it's current state, and *not* the state at the time of logging. So potentially you overwrite dest later on – A_A Dec 07 '21 at 21:34
  • 1
    From here: https://stackoverflow.com/a/36216072/5708566 `The actual implementation checks for the type of argument that is passed to log() and based on it's type, it generates a different representation.` What you actually have is an Address object which log is choosing to represent that way. – windowsill Dec 07 '21 at 22:46

0 Answers0