简体   繁体   English

Typescript:无法将默认参数值设置为 false

[英]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 ,我只想在需要时将freehand的值设置为true ,否则我希望它为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]

You just need to set the default value of freehand no need for ?你只需要设置 freehand 的默认值就可以了 不需要? 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两种方式都会给出相同的结果,但它们调用 initializeInteraction 的方式不同

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

Do you really need to wrap type and freehand up in the opts object?您真的需要在opts对象中包装typefreehand up 吗?

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 .将适用于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.这将允许您单独传递一个值,或同时传递两个值。

{ 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.幸运的是,默认情况下freehand的值将是未定义的(假的)。

You can safely replace this with您可以安全地将其替换为

initializeInteraction(opts: { type?: string; freehand?:boolean }) {
    // ...
    if (opts.freehand) {
        // Do stuff
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM