2

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 ?

2 Answers 2

5

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

modalSubmit = (autoSave: boolean = false) => {
   ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.