-3

I have a function:

public static async myFunc(
    x: 500,
    y: boolean = true,
    z = 1000,
)

I want to call the function without passing in y

myFunc(1000, 2000);

But getting an error that 2000 is not a boolean

Gurmukh Singh
  • 1,875
  • 3
  • 24
  • 62
  • 3
    that won't be valid declaration of the function in **javascript**, will it? try `myFunc(1000, undefined, 2000);` - funciton arguments are positional - even with defaults – Bravo Aug 18 '21 at 10:38
  • 2
    Man is this even javasctipt? – Adil Bimzagh Aug 18 '21 at 10:42
  • @AdilBimzagh it isn't. It's most likely TypeScript but it might also be Flow. – VLAZ Aug 18 '21 at 10:45
  • 1
    You can change the order of the arguments and make it `myFunc(x, z, y) {}` and this way you can call it like this `myFunc(1000, 2000)` – Adil Bimzagh Aug 18 '21 at 10:47

2 Answers2

0

What about using object destructuring:

function myFunc({
    x = 500,
    y = true,
    z = 1000,
})
kgreen
  • 518
  • 6
  • 13
0

You can pass object as argument into your function.

const myFunc = ({ x, y, z }) => {
  console.log(x);
  console.log(y);
  console.log(z);
};

myFunc({ x: 500, y: false, z: 1000 });

Stackblitz example