Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert from Javascript to Typescript

How would I convert this function from Javascript to Typescript?

var ToggleSwitch = this.ToggleSwitch || (function () {
//Code
}

I know the var ToggleSwtich part can be written as export class ToggleSwitch {}, but I'm not sure about the rest of the line.

like image 517
sir_thursday Avatar asked May 16 '26 01:05

sir_thursday


1 Answers

If you write a module, you'll end up with nearly identical code...

module ToggleSwitch {

}

Modules can be extended in TypeScript. The only difference is the compiler will know whether the module is already declared or not, which saves you the test.

To take this example further, here is an example with ToggleSwitch declared twice, with different contents. You'll notice that you can access all the contents. This can be split across multiple files:

module ToggleSwitch {
    export class a {
        go() {
            alert('a');
        }
    }
}

module ToggleSwitch {
    export class b {
        go() {
            alert('b');
        }
    }
}

var a = new ToggleSwitch.a();
var b = new ToggleSwitch.b();

a.go();
b.go();
like image 131
Fenton Avatar answered May 17 '26 13:05

Fenton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!