0

I defined a function like this in Javascript:

 checkButton (elem, expectedPresent, expectedEnabled) {
        var id = elem.locator_.value;
        var title = this.getTitle(id);
        expectedPresent = typeof expectedPresent !== 'undefined' ? expectedPresent : true;
        expectedEnabled = typeof expectedEnabled !== 'undefined' ? expectedEnabled : true;
        var enabledCheck = expectedEnabled ? "enabled" : "disabled";
        it(title + ' - Check it is ' + enabledCheck, function () {
            expect(elem.isPresent()).toBe(expectedPresent);
            expect(elem.isEnabled()).toBe(expectedEnabled);
        });
    }

The two trailing parameters are optional.

Now I am using Typescript. Can someone tell me if there is a better way for me to define these optional parameters using Typescript. Also how should I set the arguments so that Typescript will not give a syntax error in the call to this function when the params are not present?

1 Answer 1

1

Use = defaultValue i.e

checkButton (elem, expectedPresent = true , expectedEnabled = true ) {
        var id = elem.locator_.value;
        var title = this.getTitle(id);
        var enabledCheck = expectedEnabled ? "enabled" : "disabled";
        it(title + ' - Check it is ' + enabledCheck, function () {
            expect(elem.isPresent()).toBe(expectedPresent);
            expect(elem.isEnabled()).toBe(expectedEnabled);
        });
    }
2
  • Thanks. Is there also a way that I can specify the value is a boolean as well as the default ?
    – user3568783
    Commented Jul 16, 2014 at 9:26
  • 2
    @marifemac expectedPresent:boolean = true but since you are assigning true to it, it's not required. TypeScript will infer the type for you
    – basarat
    Commented Jul 16, 2014 at 10:00