Reputation: 3852
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
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