1

I have two javascript classes, one called AmazingMultiplier and one called AmazingAdder. I am struggling with writing typescript definition of AmazingAdder. The javascript file, looks like the following:

MyModule.AmazingAdder = function(options){
     //some amazing code

 };

MyModule.AmazingAdder.Statuses={
   success: 1,
   failure: 2
};

I am not sure how to declare the statuses object in my typescript definitions file. My d.ts file looks like:

declare module MyModule
{
  export class AmazingMultiplier
  {
       result: number;
       constructor(params: any?)
  }

 export class AmazingAdder
  {
     constructor(params: any?)
     class Statuses
     {
        success: number,
        failure: number
     }
  }

}

However I get unexpected token for 'class' where Statuses are defined. Any assistance would be appreciated. I have read through the tutorials but don't understand how I would do this.

k29
  • 641
  • 6
  • 26
  • I don't think you can create nested classes – Aaron Powell Feb 25 '15 at 10:55
  • possible duplicate of [Any way to nest classes in typescript?](http://stackoverflow.com/questions/13495107/any-way-to-nest-classes-in-typescript) – Qantas 94 Heavy Feb 25 '15 at 10:57
  • I don't really understand what you're trying to do. Can you be more specific as to what you're trying to achieve? – George Nemes Feb 25 '15 at 11:45
  • @George Nemes. It is a contrived example. All I want to do is learn how to declare a class within another class in typescript. The duplicate link has helped though and seems to work – k29 Feb 25 '15 at 12:07

1 Answers1

3

You can create a class and module with the same name. Also, you'll want to use an enum for Statuses:

declare module MyModule {
    class AmazingAdder {
        constructor(params?: any);
    }
    module AmazingAdder {
        enum Statuses {
            success = 1,
            failure = 2,
        }
    }
}
David Sherret
  • 101,669
  • 28
  • 188
  • 178
  • 1
    There are other potential solutions. But this is the most idiomatic one that I would do myself – basarat Feb 25 '15 at 23:56