簡體   English   中英

為什么 TypeScript 會拋出錯誤:TS2339:“EventTarget”類型上不存在屬性“錯誤”

[英]Why does TypeScript throw the error: TS2339: Property 'error' does not exist on type 'EventTarget'

我嘗試用 IndexedDB 為一家小書店編寫 angular 應用程序。 我將索引“isbn”設置為唯一,現在我嘗試引發錯誤,因為我想告訴用戶 isbn 必須是唯一的並且想要顯示錯誤消息。

我可以console.log(e)並得到以下 output:

事件

   {isTrusted: true, type: "error", 
   target: IDBRequest, currentTarget: IDBRequest, eventPhase: 2, …}

isTrusted: true
type: "error"
target: IDBRequest
result: undefined

錯誤:DOMException:無法將鍵添加到索引“isbn”:至少一個鍵不滿足唯一性要求。

問題:

我不能寫console.log(e.target.error)因為 TypeScript 說: TS2339: 類型 'EventTarget' 上不存在屬性 'error'

為什么?

#

這是方法:

添加項目() {

 const request = window.indexedDB.open(this.database.name); request.onsuccess = event => { const item = { title: '', isbn: 1, descrition: '', rating: 1 }; const transaction = request.result.transaction(['books'], 'readwrite'); const objectStore = transaction.objectStore('books'); const objectStoreRequest = objectStore.add(item); objectStoreRequest.onerror = e => { console.log(e.target.error); // Here is the error TS2339: Property does not exist }; }; request.onerror = event => { console.log('Error adding item'); };

}

這是 TypeScript 中的一個錯誤,請參閱https://github.com/microsoft/TypeScript/issues/30669

您可以通過強制轉換為any或使用// @ts-ignore或其他方式來解決它。

暫無
暫無

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

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