greena13 / react-hotkeys

Declarative hotkey and focus area management for React
https://github.com/greena13/react-hotkeys
ISC License
2.15k stars 160 forks source link

[BUG] ExtendedKeyMapOptions extends KeyMapOptions #287

Open EasierLikeThis opened 4 years ago

EasierLikeThis commented 4 years ago

Describe the bug The KeySequence type is defined in such a way that it's difficult to define an object of type ExtendedKeyMapOptions without the use of the as keyword. For example:

export const globalKeyMap = {
  ZOOM_IN_OUT: {
    name: "Zoom video in / out",
    sequences: ["shift+=", "shift+-"]
  },
};

will result in the following:

No overload matches this call.
  Overload 1 of 2, '(props: Readonly<GlobalHotKeysProps>): GlobalHotKeys', gave the following error.
    Type '{ ZOOM_IN_OUT: { name: string; sequences: string[]; }; }' is not assignable to type 'KeyMap'.
      Property 'ZOOM_IN_OUT' is incompatible with index signature.
        Type '{ name: string; sequences: string[]; }' is not assignable to type 'KeySequence'.
          Type '{ name: string; sequences: string[]; }' is missing the following properties from type 'ExtendedKeyMapOptions': sequence, action
  Overload 2 of 2, '(props: GlobalHotKeysProps, context?: any): GlobalHotKeys', gave the following error.
    Type '{ ZOOM_IN_OUT: { name: string; sequences: string[]; }; }' is not assignable to type 'KeyMap'.  TS2769

I believe removing the extends from ExtendedKeyMapOptions would provide the desired type structure needed.

How are you using react hotkeys components? (HotKeys, GlobalHotKeys, IgnoreKeys etc) Both HotKeys and GlobalHotKeys (but I believe this issue is present when defining any KeyMap).

Expected behavior I should be able to define my KeySequence object without the need to cast or use the as keyword.

Platform (please complete the following information):

Are you willing and able to create a PR request to fix this issue? Yes