简体   繁体   English

打字稿定义:如何在类中声明类

[英]Typescript definitions: how to declare class inside class

I have two javascript classes, one called AmazingMultiplier and one called AmazingAdder. 我有两个javascript类,一个叫做AmazingMultiplier,另一个叫AmazingAdder。 I am struggling with writing typescript definition of AmazingAdder. 我正在努力编写AmazingAdder的打字稿定义。 The javascript file, looks like the following: javascript文件如下所示:

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: 我的d.ts文件如下所示:

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. 我已经通读了教程,但不明白该怎么做。

You can create a class and module with the same name. 您可以使用相同的名称创建一个类和模块。 Also, you'll want to use an enum for Statuses : 另外,您将要为Statuses使用一个枚举:

declare module MyModule {
    class AmazingAdder {
        constructor(params?: any);
    }
    module AmazingAdder {
        enum Statuses {
            success = 1,
            failure = 2,
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM