TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Broken IndexedDB event typings in 3.1.5

Open dpogue opened this issue 7 years ago • 4 comments

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;
}

dpogue avatar Nov 01 '18 21:11 dpogue

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.

weswigham avatar Nov 02 '18 04:11 weswigham

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.

sandersn avatar Jul 17 '19 14:07 sandersn

It can be write like this

let dbreq = indexedDB.open('test', 1);

dbreq.onsuccess = function (evt) {
  let database = dbreq.result;
}

tonitrnel avatar Oct 31 '19 02:10 tonitrnel

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?

tif-calin avatar Dec 11 '25 01:12 tif-calin