0

I have a Typescript project where I am passing variables between two methods, the method that receives the information has two optional variables and I want to inform the second optional one.

How can I tell what variable I'm passing?

This is the method that sends:

function testOne() {
  let data = 'testOne';
  let data2 = 'testTwo';
  let data4 = 'testFour';
  testTwo(data,data2,data4);
}

This is the method you receive:

function testTwo(data: string, data2: string, data3?: string, data4?: string) {
  let result = `
    ${data} -- ${data2} -- ${data3 || ''} -- ${data4 || ''}
  `;
}

This is the result:

testOne -- testTwo -- testFour --

This is what I need to get:

testOne -- testTwo --  -- testFour
build
  • 37
  • 6
  • 3
    Forget TS, that's not how _JS_ works. You're passing three positional arguments, so they're `data`, `data2` and `data3` (and `data4` is left `undefined`). Maybe what you want is `testTwo(data, data2, undefined, data4)`? – jonrsharpe Nov 14 '22 at 15:36
  • @jonrsharpe yes, that's what I want, but is there a way or is it possible to tell it what variables I'm passing it? – build Nov 14 '22 at 15:38
  • That's not how _functions_ work, they don't care what name (if any, it could be `testTwo('testOne', 'testTwo', 'testFour');`) you thought the value had when you passed it. And JS doesn't have named/keyword arguments at all. There are some workarounds on the linked duplicate. – jonrsharpe Nov 14 '22 at 15:42
  • @build If you really want, I hope need to design something like this. function testTwo(data: string, data2: string, options?: { data3?: string, data4?: string }) { let result = ` ${data} -- ${data2} -- ${data3 || ''} -- ${data4 || ''} `; } – ARUNRAJ Nov 14 '22 at 15:45
  • testTwo has 4 values and since you're only 3 passing values 4th value undefined and replace with empty string. – Dreamy Player Nov 14 '22 at 15:45

0 Answers0