Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript : Can't set default parameter value as false

I've a method which has some optional parameters, like this ,

initializeInteraction(opts: { type?: string; freehand?:boolean= false }) {
    this._draw = this.drawService.initDraw({ drawtype: opts.type });
    this._drawInteraction = this._draw.interaction;
    this.mapService.addVector(this._draw.vector);
    this.mapService.addInteraction(this._drawInteraction);
  } 

I want to set the value of freehand as true only when needed , otherwise I want it as false,

but when i declare this

initializeInteraction(opts: { type: string; freehand?:boolean= false }) {}

Im getting an error as

[ts] A type literal property cannot have an initializer. [1247]
like image 873
AhammadaliPK Avatar asked Dec 05 '18 07:12

AhammadaliPK


3 Answers

You just need to set the default value of freehand no need for ? it's already optional consider this

function initializeInteraction(type: string, freehand: boolean = false) {
 console.log(type,freehand);
 // your magic
}

initializeInteraction('something');
initializeInteraction('something', false);
initializeInteraction('something', true);

the only advantage of making the parameters as object is you can pass them with different order

function initializeInteraction(opt:{ type:string , freehand?:boolean}) {
  let { type, freehand = false } = opt;
  console.log(type,freehand); 
  // your magic
}

you can short the function above like this

function initializeInteraction({type,freehand=false }: {type:string,freehand?:boolean}) {
  console.log(type,freehand);
  // your magic
 }

pass the parameter as object

initializeInteraction({ type: 'something', freehand: false });
initializeInteraction({freehand: false, type: 'something' });
initializeInteraction({type: 'something' });

both ways will give the same result ๐Ÿ‘๐Ÿ‘ , but they call initializeInteraction differently

f('') ,f('',true) or ({type:'',freehand:true}) f({freehand:true,type:''}) , f({type:''})

like image 97
Muhammed Albarmavi Avatar answered Oct 17 '22 10:10

Muhammed Albarmavi


Do you really need to wrap type and freehand up in the opts object?

I'd suggest this:

initializeInteraction(type: string, freehand?: boolean = false) {
    this._draw = this.drawService.initDraw({ drawtype: type });
    this._drawInteraction = this._draw.interaction;
    this.mapService.addVector(this._draw.vector);
    this.mapService.addInteraction(this._drawInteraction);
}

would work for the current implementation of initializeInteraction.

Edit:

The other option would be to use overloads...

initializeInteraction(type: string);
initializeInteraction(freehand: boolean);
initializeInteraction(type: string, freehand: boolean);
initializeInteraction(param1: string | boolean, param2: boolean = false) {
    //type checking and implementation here...
}

This would allow you to pass either one of your values alone, or both.

like image 3
Hayden Hall Avatar answered Oct 17 '22 09:10

Hayden Hall


{ type: string; freehand?: boolean = false }

This type literal performs the same role as an interface and therefore can't provide a default value. Fortunately, the value of freehand will be undefined (falsey) by default.

You can safely replace this with

initializeInteraction(opts: { type?: string; freehand?:boolean }) {
    // ...
    if (opts.freehand) {
        // Do stuff
    }
}
like image 1
Vincent Avatar answered Oct 17 '22 08:10

Vincent