1
export default class Area {
    param1: String;
    param2: String;
    param3: String;

    constructor(
      param1: String,
      param2: String,
      param3: String {
        this.param1 = param1;
        this.param2 = param2;
        this.param3 = param3;        
    }
}    
const area1 = new Area("test1");    

I can call constructor without param2, param3. I am going to call constructor without only param2.

// param1="test1"
const area2 = new Area("test1", ,"test3");

Is it correct?

nexdev
  • 195
  • 11

2 Answers2

1

This has already beed asked and answered here enter link description here

But to sum it up, you should write undefined, instead of the empty place between the two commas.

Also, if you want to declare a parameter as optional, you should do it using ?.

Like this: param2?: String,

Tomas Loksa
  • 95
  • 1
  • 10
1

Only the rightmost arguments can be omitted when a function is called. This means that if you want to omit the second argument you must also omit the third argument.

However, an omitted argument when the function is called is nothing more than undefined used by the interpreter to initialize the argument inside the function.

You can do the same for the arguments you don't want to pass:

const area2 = new Area("test1", undefined, "test3");
axiac
  • 68,258
  • 9
  • 99
  • 134