Reputation: 4463
I am currently writing a class that I would like to bundle with an enumerator.
example code:
class MyClass {
static enum MyType {
State1, State2, State3
};
}
This way when I have some method foo I can define it as follows:
function CreateMyClass(type: MyClass.MyType) {
...
}
Perhaps I do not have the syntax correct (or maybe I'm going about this in the wrong way)? Is this possible, or is there a workaround so that I do not have to create an interface for a static variable in order to accept the enum type in a method parameter?
Upvotes: 2
Views: 575
Reputation: 17875
What about module?
module M {
export enum MyType {
State1, State2, State3
};
export class MyClass {
constructor(param: string) {
alert(param);
}
}
}
function CreateMyClass(type: M.MyType): M.MyClass {
if (type === M.MyType.State1)
return new M.MyClass("Hi!");
else
return new M.MyClass("Hello!");
}
Upvotes: 1
Reputation: 251062
You can't define the enum within the class, but you can add it elsewhere and use it within the class.
enum MyType {
State1, State2, State3
};
class MyClass {
constructor(private type: MyType) {
}
}
var myInstance = new MyClass(MyType.State2);
Upvotes: 1