简体   繁体   English

导出全局声明会破坏现有的自定义模块声明

[英]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.

相关问题 角度模块名称全局声明和重用 - angular module name global declaration and reuse 键入:带有类原型的JS模块导出类实例的类型声明 - Typings: Type declaration for JS module exporting instance of class, with prototype of the class 导出时,获取导出声明可能只出现在 JavaScript 模块的顶层 - Getting export declarations may only appear at top level of a module in JavaScript when exporting 导出变量时,如何解决“ SyntaxError:导出声明​​只能出现在模块的顶层”的问题? - How to resolve 'SyntaxError: export declarations may only appear at top level of a module' when exporting variables? 向 javascript 的数组 class 添加自定义函数会破坏数组声明 - Adding custom functions to javascript's Array class breaks array declaration 带自定义装载器的热模块更换中断 - Hot Module Replacement breaks w/ custom loader 如何为导出为全局的javascript模块创建类型声明? - How to create typings declaration for javascript module exported as a global? 导入现有 .jsx 文件时“找不到模块的声明文件” - "Could not find a declaration file for module" when importing existing .jsx files JavaScript模块的TypeScript声明 - TypeScript declarations for JavaScript module 自定义JavaScript至现有的Joomla核心模块 - Custom JavaScript to existing Joomla core module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM