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.

4
  • I don't think you can create nested classes Commented Feb 25, 2015 at 10:55
  • possible duplicate of Any way to nest classes in typescript? Commented Feb 25, 2015 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? Commented Feb 25, 2015 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 Commented Feb 25, 2015 at 12:07

1 Answer 1

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,
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

There are other potential solutions. But this is the most idiomatic one that I would do myself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.