【问题标题】:TS1183: implementation cannot be declared in ambient context in TypescriptTS1183:无法在 Typescript 的环境上下文中声明实现
【发布时间】:2018-11-30 00:40:29
【问题描述】:

我正在尝试创建一个全局命名空间/函数 我的代码如下所示:abc.ts

declare namespace abc {
   export function abc (): xyz {
        console.log('Hello');
        return xyz(200);
    }
}
export = abc

我做错了什么?我如何解决它 ?

【问题讨论】:

  • 摆脱declare?

标签: typescript


【解决方案1】:

如果您删除“声明”关键字,您的代码将正常工作

namespace abc {
   export function abc (): xyz {
        console.log('Hello');
        return xyz(200);
    }
}
export default abc

declare 用于告诉 TypeScript 变量已创建 别处。如果您使用声明,则不会向 JavaScript 添加任何内容 生成 - 它只是对编译器的提示。

What does 'declare' do in 'export declare class Actions'?


在您的情况下,实施可以在另一个地方。下面的代码也可以正常工作。

declare namespace abc {
   export function abc ();
}

namespace abc {
   function abc (): xyz {
        console.log('Hello');
        return xyz(200);
    }
}

export default abc

https://www.typescriptlang.org/docs/handbook/declaration-files/by-example.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 2020-05-22
    • 2020-08-16
    • 2021-05-08
    • 2020-08-16
    • 2021-07-09
    相关资源
    最近更新 更多