【问题标题】:Does a module make typescript code "Ambient"模块是否使打字稿代码“环境”
【发布时间】:2016-02-20 21:33:46
【问题描述】:

我正在尝试从一些打字稿开始。这就是我的样子:

declare module ServiceActions {
    export class MyClass{
        myFunction(shipmentId: number) : void {
            let test: number;
            test = 32;
        }
    }
}

当我这样做时,我得到了这个错误:

不能在环境上下文中声明实现。

当我取出模块时,它工作正常:

export class MyClass{
    myFunction(shipmentId: number) : void {
        let test: number;
        test = 32;
    }
}

我以为我读到模块就像 Typescript 中的命名空间,但这似乎使它更像是一个抽象/接口概念。

我真的因为模块而收到此错误吗?如果不是,为什么会出现这个错误?

注意:我看到了这个question and answer,它没有回答我的问题。

【问题讨论】:

    标签: typescript


    【解决方案1】:

    在这种情况下,由于declare 关键字,您处于“环境”上下文中。你想要的是在你的模块上使用export

    export module ServiceActions {
        export class MyClass{
            myFunction(shipmentId: number) : void {
                let test: number;
                test = 32;
            }
        }
    }
    

    您也可以完全省略export,这更像是一个命名空间声明。如果您使用 AMD、CommonJS 或 ES2015 之类的东西进行模块声明,则需要 export

    declare 用于指定模块以包含接口等内容。所以也许:

    declare module ServiceActions {
        interface MyInterface {
            myFunction(shipmentId: number) : void;
        }
    }
    module ServiceActions {
        export class MyClass implements MyInterface {
            myFunction(shipmentId: number) : void {
                let test: number;
                test = 32;
            }
        }
    }
    

    例如,您会在 .d.ts 文件中看到很多。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 2023-03-29
      • 2017-03-11
      • 2013-08-27
      • 1970-01-01
      • 2020-03-25
      相关资源
      最近更新 更多