简体   繁体   English

TypeScript是否支持命名空间类的类声明?

[英]Does TypeScript support class declaration for a namespaced class?

I'm converting JavaScript code to TypeScript, and stuck on the best way to convert my class definitions. 我将JavaScript代码转换为TypeScript,并停留在转换类定义的最佳方法上。

They are extended from Marionette, and the class names are namespaced. 它们是从Marionette扩展而来的,而类名是命名空间。 For example, this JavaScript using the 'xyz' namespace: 例如,此JavaScript使用'xyz'名称空间:

xyz.Marionette.AppRouter = Marionette.AppRouter.extend({
    instance_name: "AppRouter",

     functionName: function(options) {
     }
});

I tried creating this class command equivalent in TypeScript: 我尝试在TypeScript中创建等效的此类命令:

class xyz.Marionette.AppRouter extends Marionette.AppRouter {

but this syntax does not seem supported. 但此语法似乎不受支持。

Is is possible declare a class in TypeScript with namespacing? 可以在具有名称间隔的TypeScript中声明一个类吗?

The answers below (so far) are helpful, but I still don't know how to express this definition, or even if it's possible. 下面的答案(到目前为止)是有帮助的,但是我仍然不知道如何表达这个定义,甚至是可能的。

class nameSpace.Marionette.AppRouter extends Marionette.AppRouter { 类nameSpace.Marionette.AppRouter扩展了Marionette.AppRouter {

Declare the namespace and then the class: 声明名称空间, 然后声明类:

namespace nameSpace.Marionette {
    export class AppRouter {

    }
}

I tried mocking this up but had a problem referencing the base AppRouter because the namespaces are the same. 我尝试对此进行模拟,但是由于命名空间相同,因此在引用基本AppRouter时遇到了问题。 It works if I add a qualifier of "base" to the first namespace.. 如果我在第一个命名空间中添加“ base”的限定词,则该方法有效。

namespace base.Marionette {

    export class AppRouter {

        Foo() {
            return "Hello";
        }
    }
}

namespace nameSpace.Marionette {

    export class AppRouter2 extends base.Marionette.AppRouter {

        Bar() {
            var hello = this.Foo();
        }
    }
}

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

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