簡體   English   中英

為什么打字稿抱怨不可擴展性和依賴庫的只讀違規?

[英]Why does typescript complain about non-extensibility and readonly violation of dependency library?

我使用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 庫而不是 angular 服務)后會發生此錯誤。 顯然,我的代碼更改必須有某些原因導致這種情況發生,我仍然無法弄清楚它是什么。 我懷疑的一件事是,它似乎在抱怨,因為它正在處理 EventSourcePolyfill,就好像它具有來自TypeScript 的 lib.dom.d.tsEventSource 類型,因為 readyState 字段是只讀的(但我真的不不要認為我在任何地方將兩者連接起來!)。 但正如堆棧跟蹤所示,調用了 vanilla js 庫,即使在閱讀源代碼后,我也看不到這些變量在哪里被設置為只讀或不可擴展的。

原來,NgRx 凍結了它的商店。 在我們將 EventSource 對象提交到 ngrx 的存儲中后,它被深度凍結為對象。 這就是為什么它停止運行並且在商店提交后無法在運行時更新自己的屬性。 一個可能的解決方案可能是ngrx-store-freeze雖然我們還沒有嘗試過,我認為我們不打算這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM