jQuery事件的打字稿错误

时间:2019-04-12 14:17:33

标签: jquery typescript typescript-typings

我已经将项目迁移到Typescript。到目前为止,一切都很好,但是我在jQuery事件方面遇到了麻烦。

例如代码

$(document).on('cc.command',
...

Typescript引发错误

ERROR in [..]/src/modules/command-center.ts
./src/modules/command-center.ts
[tsl] ERROR in [..]/src/modules/command-center.ts(24,18)
      TS2345: Argument of type '"cc.command"' is not assignable to parameter of type 'TypeEventHandlers<Document, (this: Document, e: Event, str: string) => void, Document, Document>'.

类似地,使用代码

$('#user-input').keydown(
      function onKeydown(e:KeyboardEvent): void {
...

我遇到了错误

ERROR in [..|/src/modules/user-interface.ts
./src/modules/user-interface.ts
[tsl] ERROR in [..|/src/modules/user-interface.ts(55,16)
      TS2345: Argument of type '(this: HTMLElement, e: KeyboardEvent) => void' is not assignable to parameter of type 'false | EventHandlerBase<HTMLElement, KeyDownEvent<HTMLElement, null, HTMLElement, HTMLElement>> | undefined'.
  Type '(this: HTMLElement, e: KeyboardEvent) => void' is not assignable to type 'EventHandlerBase<HTMLElement, KeyDownEvent<HTMLElement, null, HTMLElement, HTMLElement>>'.
    Types of parameters 'e' and 't' are incompatible.
      Type 'KeyDownEvent<HTMLElement, null, HTMLElement, HTMLElement>' is missing the following properties from type 'KeyboardEvent': code, location, repeat, getModifierState, and 20 more.

还有一个,带有:

$('#user-name').on('blur',
..

我得到了

ERROR in [..|/src/modules/user-interface.ts
./src/modules/user-interface.ts
[tsl] ERROR in [..|/src/modules/user-interface.ts(65,24)
      TS2345: Argument of type '"blur"' is not assignable to parameter of type 'TypeEventHandlers<HTMLElement, (this: HTMLElement, e: KeyboardEvent) => void, HTMLElement, HTMLElement>'.

我已经安装了jQuery类型定义,并且在strictFunctionTypes中将false设置为tsconfig.json时也会发生相同的错误

如何解决这些错误?

我应该提到我正在使用npm包打字稿3.1.6,jquery 3.4.0和@ types / jquery 3.3.29。

0 个答案:

没有答案