I have some simple popstate code for an SPA as follows:
module myModule {
$(function () {
$(window).on("popstate", function(e) {
if (e.originalEvent.state !== null) {
// code removed
}
});
});
}
(The use of originalEvent is due to answers like this: popstate returns event.state is undefined)
This gives a compile error:
Property 'state' does not exist on type 'Event'.
e is a JQueryEventObject type and originalEvent is an Event type.
Now I've managed to work around the problem by defining this interface:
interface EventWithState extends Event {
state: any;
}
(*I doubt "any" is really my best option here - if anyone knows what type state should be let me know, and more importantly how do I find that out for myself in such cases)
And I changed my above code like so:
module myModule {
$(function () {
$(window).on("popstate", function(e) {
if ((<EventWithState>e.originalEvent).state !== null) {
// code removed
}
});
});
}
OK that works fine now and compiles to the original code I wanted. But it seems like an awkward workaround.
Is this the acceptable method to do this? Is something missing from typescript definitions? Or is there something I am missing.
Furthermore using e.state
also doesn't work, however editing jquery.d.ts (from DefinitelyTyped) and adding state: any;
to BaseJQueryEventObject fixes it in a similar way to my workaround. This is what makes me think something is missing from definitions for typescript regarding the Event state.