[英]Exporting global declaration breaks existing custom module declarations
In my NextJS web app, I have an existing set of custom typings for packages I've installed that do not have types available.在我的 NextJS web 应用程序中,我有一组现有的自定义类型,用于我安装的没有可用类型的包。 I needed to extend my
window
with a module from a third party script.我需要使用来自第三方脚本的模块来扩展我的
window
。
So I extended and exported the Window
interface in my global.d.ts
file.所以我在我的
global.d.ts
文件中扩展并导出了Window
接口。 Once I did that, one of the packages that I've declared any
types globally for started giving me the infamous Could not find a declaration file for module but it exists (TS7016)
error.一旦我这样做了,我已经在全局范围内声明
any
类型的包之一开始给我臭名昭著的Could not find a declaration file for module but it exists (TS7016)
错误。 When I removed the new export of the new global window
type, TS7016
went away`.当我删除新的全局
window
类型的新导出时, TS7016
了`。
How can I have both custom typings for 3rd party modules along with a exported custom global type?我怎样才能同时拥有 3rd 方模块的自定义类型和导出的自定义全局类型?
// global.d.ts
declare module '@package-maintainer/*';
declare module '@package-maintainer-2/*';
declare module 'some-package';
declare module 'some-package-2';
export declare global {
interface Window {
thirdPartyModule: {
aMethod: () => void
};
}
}
After a handful of testing, what I believe the issue was was the exporting of a module, which turned the global.d.ts
file itself into a module, and that is not compatible with declare module
.经过一些测试,我认为问题出在模块的导出上,这将
global.d.ts
文件本身变成了一个模块,并且与declare module
不兼容。 I'm not exactly sure the reasoning behind this.我不确定这背后的原因。 I'd have to dig into this further.
我必须进一步深入研究。
The solution here was to separate the custom type definitions into two files, one for global definitions and one for module definitions:这里的解决方案是将自定义类型定义分成两个文件,一个用于全局定义,一个用于模块定义:
// global.d.ts
export declare global {
interface Window {
thirdPartyModule: {
aMethod: () => void
};
}
}
// modules.d.ts
declare module '@package-maintainer/*';
declare module '@package-maintainer-2/*';
declare module 'some-package';
declare module 'some-package-2';
With these separated, but both included in my tsconfig
, I was able to extend my window
object and keep my custom coercion to any
for my third-party packages.将它们分开,但都包含在我的
tsconfig
中,我能够扩展我的window
object 并为我的第三方包保留我对any
的自定义强制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.