gpt4 book ai didi

typescript - TS1183 : implementation cannot be declared in ambient context in Typescript

转载 作者:行者123 更新时间:2023-12-03 08:58:32 34 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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

declare is used to tell TypeScript that the variable has been created elsewhere. If you use declare, nothing is added to the JavaScript that is generated - it is simply a hint to the compiler.

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

关于typescript - TS1183 : implementation cannot be declared in ambient context in Typescript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53012438/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com