Broken IndexedDB event typings in 3.1.5
TypeScript Version: 3.1.5
Search Terms: indexedDB
Code
let dbreq = indexedDB.open('test', 1);
dbreq.onsuccess = function (evt) {
let database = evt.target.result;
}
Expected behavior:
evt.target.result should be an IDBDatabase
Actual behavior: TS2339: Property 'result' does not exist on type 'EventTarget'.
Playground Link: http://www.typescriptlang.org/play/index.html#src=let%20dbreq%20%3D%20indexedDB.open('test'%2C%201)%3B%0D%0A%0D%0Adbreq.onsuccess%20%3D%20function%20(evt)%20%7B%0D%0A%20%20%20%20let%20database%20%3D%20evt.target.result%3B%0D%0A%7D
Related Issues: This is caused by the updates resulting from https://github.com/Microsoft/TypeScript/issues/25547
It works if you add a cast:
let dbreq = indexedDB.open('test', 1);
dbreq.onsuccess = function (evt) {
let database = (evt.target as IDBOpenDBRequest).result;
}
PRs welcomed. You can find more information about contributing lib.d.ts fixes at https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes.
Following on to @weswigham’s comment, this is now easily fixed as an extension to https://github.com/microsoft/TSJS-lib-generator/pull/707; ProgressEvent now has a type parameter that controls the type of target, so it’s just a matter of passing the type in from the subclass declaration.
It can be write like this
let dbreq = indexedDB.open('test', 1);
dbreq.onsuccess = function (evt) {
let database = dbreq.result;
}
The workaround technically works but TypeScript should be able to handle idiomatic code that's even in MDN docs. There are many use cases where it's impractical to keep track of access to the original IDBOpenDBRequest object and event.target.result is the preferred access route
IndexedDB has been widely available since early 2020 when Edge finally went Chromium. Any update on fixing the event type?