Typescript casting object's property

后端 未结 2 1674
予麋鹿
予麋鹿 2021-02-13 02:54

I\'m working with indexeddb and typescript. My issue is that TS doesn\'t seem to be able handle the event.target.result property. Case in point:

req         


        
2条回答
  •  無奈伤痛
    2021-02-13 03:20

    If you are looking for a oneliner you can cast it by adding some extra parenthesis like so:

    indexedDB.open("test", 1).onsuccess = (ev) => {
        var result: IDBDatabase = (ev.target).result;
    }
    

    Also notice the : IDBDatabase because result is typed as any in the Typescript definition file. It isn't needed but using it as an "any" type would mean no typechecking by the compiler.

    Now you can use the result like you want with the methods available as defined here: http://www.w3.org/TR/IndexedDB/#database-interface

提交回复
热议问题