typescript - 为什么打字稿抱怨依赖库的不可扩展性和只读违规?
问题描述
我使用event-source-polyfill库作为依赖,版本 ^1.0.12。调用 addEventListener 方法时,出现以下错误:
ERROR TypeError: Cannot add property message, object is not extensible
at EventSourcePolyfill.EventTarget.addEventListener (c:\git\my-project\node_modules\event-source-polyfill\src\eventsource.js:607:1)
at MapSubscriber.project (c:\git\my-project\src\app\notifications\store\notifications.effects.ts:56:44)
at MapSubscriber._next (c:\git\my-project\node_modules\rxjs\_esm2015\internal\operators\map.js:29:1)
at MapSubscriber.next (c:\git\my-project\node_modules\rxjs\_esm2015\internal\Subscriber.js:49:1)
at WithLatestFromSubscriber._next (c:\git\my-project\node_modules\rxjs\_esm2015\internal\operators\withLatestFrom.js:57:1)
at WithLatestFromSubscriber.next (c:\git\my-project\node_modules\rxjs\_esm2015\internal\Subscriber.js:49:1)
at FilterSubscriber._next (c:\git\my-project\node_modules\rxjs\_esm2015\internal\operators\filter.js:33:1)
at FilterSubscriber.next (c:\git\my-project\node_modules\rxjs\_esm2015\internal\Subscriber.js:49:1)
at ScannedActionsSubject.next (c:\git\my-project\node_modules\rxjs\_esm2015\internal\Subject.js:39:1)
at SafeSubscriber._next (https://localhost:4200/my-project/vendor.js:148530:32)
调用 EventSourcePolyfill.close() 时,我也会收到此错误:
ERROR TypeError: Cannot assign to read only property 'readyState' of object '[object Object]'
at EventSourcePolyfill.close [as _close] (c:\git\my-project\node_modules\event-source-polyfill\src\eventsource.js:918:1)
at EventSourcePolyfill.close (c:\git\my-project\node_modules\event-source-polyfill\src\eventsource.js:996:1)
at SwitchMapSubscriber.project (c:\git\my-project\src\app\notifications\store\notifications.effects.ts:23:45)
at SwitchMapSubscriber._next (c:\git\my-project\node_modules\rxjs\_esm2015\internal\operators\switchMap.js:28:1)
at SwitchMapSubscriber.next (c:\git\my-project\node_modules\rxjs\_esm2015\internal\Subscriber.js:49:1)
at WithLatestFromSubscriber._next (c:\git\my-project\node_modules\rxjs\_esm2015\internal\operators\withLatestFrom.js:57:1)
at WithLatestFromSubscriber.next (c:\git\my-project\node_modules\rxjs\_esm2015\internal\Subscriber.js:49:1)
at FilterSubscriber._next (c:\git\my-project\node_modules\rxjs\_esm2015\internal\operators\filter.js:33:1)
at FilterSubscriber.next (c:\git\my-project\node_modules\rxjs\_esm2015\internal\Subscriber.js:49:1)
at ScannedActionsSubject.next (https://localhost:4200/my-project/vendor.js:288904:25) {stack: 'TypeError: Cannot assign to read only propert…t:4200/my-project/vendor.js:288904:25)', message: 'Cannot assign to read only property 'readyState' of object '[object Object]''}
这个库曾经使用这个相同的版本。此错误发生在代码中的一些重构之后(使用 NgRx 库而不是角度服务)。显然,我的代码更改一定有什么导致这种情况发生,我仍然无法弄清楚它是什么。我怀疑的一件事是它似乎在抱怨,因为它正在处理 EventSourcePolyfill 就好像它具有来自 TypeScript 的 lib.dom.d.ts 的 EventSource 类型,因为那里的 readyState 字段是只读的(但我真的没有不认为我在任何地方都连接了这两个!)。但是正如堆栈跟踪所显示的那样,调用了 vanilla js 库,并且即使在阅读了源代码之后,我也看不到这些变量在哪里被设为只读或不可扩展。
解决方案
事实证明,NgRx 冻结了它的存储。在我们将 EventSource 对象提交到 ngrx 的存储中后,它被深度冻结为对象。这就是为什么它停止运行并且在商店提交后无法在运行时更新自己的属性。一个可能的解决方案可能是ngrx-store-freeze虽然我们还没有尝试过,我认为我们不打算这样做。
推荐阅读
- java - 2个插件在pom中不能同时工作
- amazon-web-services - AWS Device Farm - 在设备上禁用教程
- android - LiveData 和转换使用
- haskell - 在haskell中为每个列表元素编号并格式化文本
- firebase - 使用“firebase serve”时出现“EACCES:权限被拒绝”
- java - 在spring data jpa的同一个事务中修改不可见,为什么在同一个事务中修改后我得到null?
- python - 使用装饰器保存 matplotlib 图形;保存的输出为空白
- php - Laravel 5.5 测试删除 api 路由的 ModelNotFoundException 异常
- r - 将字符数据构造成数据框
- python - 填充 DataFrame Pandas Python