Typescript casting object's property Typescript casting object's property typescript typescript

Typescript casting object's property


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 = (<IDBOpenDBRequest>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


You can also cast using the 'as' keyword like this

interface MyCustomUserInterface {    _id: string;    name: string;}const userID = (req.user as MyCustomUserInterface)._id...

Cheers!