Static class

Topics: General, Language Specification
Sep 4, 2013 at 10:33 PM
Hello,

i'm fairly new to typescript, so please excuse me if i'm asking for something stupid.

I wanted to create a class with only static functions, so in c# that would be a static class, in javascript that would be a var with some functions attached to it.

So i tried to create this 'static class' in typescript. The keyword static is not valid before "class" so i created a class like this:
class Multiplication {
    static multiplyAmount:number = 2;
    static multiply(amount:number)
    {
        return amount * this.multiplyAmount;
    }
}

alert(Multiplication.multiply(3).toString());
Converted to javascript this becomes
var Multiplication = (function () {
    function Multiplication() {
    }
    Multiplication.multiply = function (amount) {
        return amount * this.multiplyAmount;
    };
    Multiplication.multiplyAmount = 2;
    return Multiplication;
})();

alert(Multiplication.multiply(3).toString());
Now i don't like the fact that a function is being created here. It's not like a static class has to be instantiated. As far as i've been looking around i haven't seen anything like a static class in TypeScript, so i propose the following (a real static class):
static class Multiplication {
    static multiplyAmount:number = 2;
    static multiply(amount:number)
    {
        return amount * this.multiplyAmount;
    }
}

alert(Multiplication.multiply(3).toString());
And in javascript this becomes
            var Multiplication = (function () {
                var Multiplication = {};
                Multiplication.multiply = function (amount) {
                    return amount * this.multiplyAmount;
                };
                Multiplication.multiplyAmount = 2;
                return Multiplication;
            })();

            alert(Multiplication.multiply(3).toString());
In the case a class has the keyword static, all the functions and variables inside should also have the keyword static.
In JavaScript no function is defined but an empty object is created instead.

Would this be possible in any case?
Coordinator
Sep 4, 2013 at 10:51 PM
A module covers all the use cases of a static class, and has the code gen you'd expect (member initialization without a constructor function).