简体   繁体   English

Typescript + jQuery:使用事件状态的正确方法?

[英]Typescript + jQuery: correct way to use event state?

I have some simple popstate code for an SPA as follows: 我为SPA提供了一些简单的popstate代码,如下所示:

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 ) (使用originalEvent的原因是这样的答案: popstate返回event.state是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. e是JQueryEventObject类型,originalEvent是Event类型。

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; 此外,使用e.state也不起作用,但是编辑jquery.d.ts(来自DefinitelyTyped)并添加state: any; to BaseJQueryEventObject fixes it in a similar way to my workaround. BaseJQueryEventObject的修复方法与我的解决方法类似。 This is what makes me think something is missing from definitions for typescript regarding the Event state. 这就是让我认为关于事件状态的打字稿定义中缺少某些内容的原因。

Thanks to Pilot for sharing the answer to another question, though it seemed unrelated at first, it did hold the clues and links for me to work out that I could cast to PopStateEvent rather than my own EventWithState. 感谢Pilot共享了另一个问题的答案,尽管起初似乎并不相关,但它确实为我提供了线索和链接,使我可以将其转换为PopStateEvent而不是自己的EventWithState。

module myModule {
    $(function () {
        $(window).on("popstate", function(e) {
            if ((<PopStateEvent>e.originalEvent).state !== null) {
                // code removed
            }
        });
    });
}

https://developer.mozilla.org/en-US/docs/Web/API/Event https://developer.mozilla.org/en-US/docs/Web/API/Event

https://developer.mozilla.org/en-US/docs/Web/API/PopStateEvent https://developer.mozilla.org/en-US/docs/Web/API/PopStateEvent

And for the record, lib.d.ts does use "any" as the type for state: 出于记录目的,lib.d.ts确实使用“ any”作为状态类型:

interface PopStateEvent extends Event {
    state: any;
    initPopStateEvent(typeArg: string, canBubbleArg: boolean, cancelableArg: boolean, stateArg: any): void;
}

https://typescript.codeplex.com/SourceControl/latest#bin/lib.d.ts https://typescript.codeplex.com/SourceControl/latest#bin/lib.d.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM