amin
amin

Reputation: 3852

How to pass an argument to a TypeScript module?

Is it possible to pass a variable to TypeScript modules?

When using a class, we can pass arguments to the constructor:

class validator {
    constructor(public regex: RegExp) { }
    ok = (s: string) => this.regex.test(s);
}

But, how can I pass the same argument to a module?

module validator {
    var regex = /^[A-Za-z]+$/;
    export var ok = (s: string) => regex.test(s);
};

Upvotes: 1

Views: 1482

Answers (1)

zlumer
zlumer

Reputation: 7004

Export the variable you want to modify:

module validator {
    export var regex = /^[A-Za-z]+$/; // <-- export var
    export var ok = (s: string) => regex.test(s);
};

validator.regex = /.*$/;

This will make module behave similar to a static class: you have a single point of access to this variable.

Upvotes: 1

Related Questions