简体   繁体   English

Polyfill 类型定义没有被拾取

[英]Polyfill type definitions not getting picked up

I'm using TypeScript 4.4.4 and I'm trying to use this EventSource polyfill in my frontend (React): https://github.com/EventSource/eventsource .我正在使用 TypeScript 4.4.4 并且我正在尝试在我的前端(React)中使用这个 EventSource polyfill: https://github.com/EventSource/eventsource I've also installed the type definitions for that package.我还安装了 package 的类型定义。

I specifically need to add headers to the SSE initiation: eventSourceRef.current = new EventSource('/api/foo', { headers: { bar: 'buzz' } });我特别需要向 SSE 启动添加标题: eventSourceRef.current = new EventSource('/api/foo', { headers: { bar: 'buzz' } }); . . Unfortunately, the type definitions aren't being picked up.不幸的是,没有选择类型定义。 TypeScript complains if I try to add headers as a property to the EventSourceInit options, which means that the types of the baked in EventSource are still being used, not the polyfill definitions.如果我尝试将headers作为属性添加到EventSourceInit选项,TypeScript 会抱怨,这意味着仍在使用EventSource中烘焙的类型,而不是 polyfill 定义。

I suspect it might have something to do with that the polyfill package doesn't export the class itself, so I can't import the class.我怀疑这可能与 polyfill package 本身不导出 class 有关,所以我无法导入 class。 I have to import the whole package, like this: import 'eventsource/lib/eventsource-polyfill';我必须导入整个 package,如下所示: import 'eventsource/lib/eventsource-polyfill';

I've tried the following:我尝试了以下方法:

  • import the polyfill in my entry file在我的入口文件中导入 polyfill
  • import the polyfill only in the file that I need it仅在我需要的文件中导入 polyfill
  • import the type definition reference: /// <reference path='../../../../../node_modules/@types/eventsource/index.d.ts'/>导入类型定义参考: /// <reference path='../../../../../node_modules/@types/eventsource/index.d.ts'/>
  • extend the global type and try to extend the EventSourceInit interface with the new properties allowed by the polyfill, like so:扩展全局类型并尝试使用 polyfill 允许的新属性扩展EventSourceInit接口,如下所示:
export {};
declare global {
    interface EventSourceInit {
        headers?: object | undefined;
    }
}

I realize I could always just cast it as any or import using require, but I feel like if there are type definitions for this it should be possible to use them in my project.我意识到我总是可以将其转换为 any 或使用 require 导入,但我觉得如果有为此的类型定义,应该可以在我的项目中使用它们。

What am I missing here?我在这里想念什么?

Edit: I also tried the following, but none worked either:编辑:我也尝试了以下方法,但都没有:

  • import the package as shown in the type definition tests: import EventSourcePolyfill = require("eventsource/lib/eventsource-polyfill");导入 package,如类型定义测试所示: import EventSourcePolyfill = require("eventsource/lib/eventsource-polyfill"); . . This doesn't work because I'm targeting ECMAScript modules.这不起作用,因为我的目标是 ECMAScript 模块。
  • import it like this: import EventSourcePolyfill from "eventsource/lib/eventsource-polyfill";像这样导入它: import EventSourcePolyfill from "eventsource/lib/eventsource-polyfill"; . . However, when calling the constructor ( const foo = new EventSourcePolyfill('/foo) ) TypeScript complains that the expression is not constructable, as "Type 'typeof EventSource' has no construct signatures".但是,当调用构造函数 ( const foo = new EventSourcePolyfill('/foo) ) TypeScript 抱怨表达式不可构造,因为“类型'typeof EventSource'没有构造签名”。

Sigh... I found the way.叹息……我找到了路。 I started typing out the EventEmitter class in an expression and I noticed the intellisense suggesting the class coming from the package.我开始在表达式中输入EventEmitter class,我注意到智能感知提示 class 来自 package。 This is the way to import it, apparently:显然,这是导入它的方式:

import EventSource from 'eventsource';

I had been trying to import 'eventsource/lib/eventsource-polyfill' as per the instructions in the package README:我一直在尝试按照 package README 中的说明导入“eventsource/lib/eventsource-polyfill”:

Just add example/eventsource-polyfill.js file to your web page:只需将 example/eventsource-polyfill.js 文件添加到您的 web 页面:

<script src=/eventsource-polyfill.js></script>

I got confused by that and tried importing the wrong thing.我对此感到困惑并尝试导入错误的东西。 But apparently just importing the package index suffices.但显然只导入 package 索引就足够了。 I guess the simplest answer really is the best.我想最简单的答案确实是最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM