Typescript: KeyboardEventInit missing props

Created on 17 Apr 2017  ·  18Comments  ·  Source: microsoft/TypeScript

The KeyboardEvent list is props is here:

TypeScript Version:
2.1.6
Code
I'm trying to create a KeyboardEvent

    let e = new KeyboardEvent("keyup", {
      bubbles : true,
      cancelable : true,
      charCode : "1",
      key : "1",
      shiftKey : true,
      keyCode : 81
    });

Expected behavior:
No error
Actual behavior:
I get error:

ERROR in /Users/nikos/WebstormProjects/angular-finance/tests/src/app/app.component.spec.ts (33,7): Argument of type '{ bubbles: true; cancelable: true; char: string; key: string; shiftKey: true; keyCode: number; }' is not assignable to parameter of type 'KeyboardEventInit'.
  Object literal may only specify known properties, and 'char' does not exist in type 'KeyboardEventInit'.)

Because this only has 4 props:

image

Bug lib.d.ts help wanted

Most helpful comment

Any updates on this? Or any workaround for creating proper keyboard events?

All 18 comments

PRs welcomed. You can find more information about contributing lib.d.ts fixes at https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes.

Cool I will work on a fix.

Kind regards

Nikos
quantumjs.com

I've got quite a few hits in lib/ do I need to change them all?

image

The files under lib are not manually edited, they are auto-generatd from a script. You can find more information about contributing lib.d.ts fixes at https://github.com/Microsoft/TSJS-lib-generator.

Changes should be done in https://github.com/Microsoft/TSJS-lib-generator instead.

Cool thanks, I'll have a play.

I'll update the added types:

image

I can't see where this KeyboardEventInit is defined. The only thing in added types is:

    {
        "kind": "property",
        "interface": "KeyboardEventInit",
        "name": "code?",
        "type": "string"
    },

image

in overridingTypes.json, add:

    {
        "kind": "constructor",
        "interface": "KeyboardEvent",
        "signatures": ["new(type: string, eventInitDict?: KeyboardEventInit): KeyboardEvent"]
    },

I tried installing mono but am getting:
image

Any updates on this? Or any workaround for creating proper keyboard events?

I would also like to see this resolved. Any movement on this?

@kscoulter

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

Above it clearly states that keyCode, has been deprecated and should not be used. Instead, code should be used.

@ioanungurean

Instead, you should use KeyboardEvent.code, if it's implemented. Unfortunately, some browsers still don't have it, so you'll have to be careful to make sure you use one which is supported on all target browsers.

If you need to support IE/Edge you can't use code and need to still need to use keyCode.

I'm happy to make a PR fixing this if it would be welcome. Understand it is deprecated but it is still very much supported and unfortunately necessary in some cases.

Is there any progress on it

I made a PR for it ,
Hope it would be merged.

I think that Angular uses keyCode, I was trying to dispatch a KeyboardEvent in jest but it wasn't catched by the component, so I found a nice solution and it works.

Object.defineProperty(event, 'keyCode', {
  get: () => 27,
});

PR is merged.

Was this page helpful?
0 / 5 - 0 ratings