简体   繁体   中英

How can I make a default parameter in Typescript?

How can I make a Typescript function parameter have a default value ?

I have this code:

modalSubmit = (autoSave) => {

    var self = this;
    self.stateService.network('Submitting');
    self.modal.resetDisabled = true;
    self.modal.submitDisabled = true;
    autoSave = autoSave || false;

Is there a way with Typescript that I can make autoSave default to false if it's not set ?

yep: http://www.codebelt.com/typescript/javascript-default-parameters-with-typescript-tutorial/

modalSubmit = (autoSave: boolean = false) => {
   ...
}

Just for information, if you are wondering how you should check for a value in pure JS:

autoSave = autoSave !== undefined ? autoSave : false;

Of course typescript does it for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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