user1679941
user1679941

Reputation:

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 ?

Upvotes: 2

Views: 160

Answers (2)

basarat
basarat

Reputation: 275927

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.

Upvotes: 0

Jeremy Danyow
Jeremy Danyow

Reputation: 26406

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

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

Upvotes: 5

Related Questions