Skip to content
This repository has been archived by the owner on Aug 17, 2023. It is now read-only.

type error after compile #4

Closed
palindrom615 opened this issue Jul 5, 2019 · 0 comments
Closed

type error after compile #4

palindrom615 opened this issue Jul 5, 2019 · 0 comments

Comments

@palindrom615
Copy link
Contributor

palindrom615 commented Jul 5, 2019

  • I'm submitting a ...

[x] bug report
[ ] feature request
[ ] question about the decisions made in the repository
[ ] question about how to use this project

  • Summary

this is _getEventHandlers method in MaskedInput.tsx:

// MaskedInput.tsx
// ...
  _keyPressPropName() {
    if (typeof navigator !== 'undefined') {
      return navigator.userAgent.match(/Android/i)
        ? 'onBeforeInput'
        : 'onKeyPress';
    }
    return 'onKeyPress';
  }

  _getEventHandlers() {
    return {
      onChange: this._onChange,
      onKeyDown: this._onKeyDown,
      onPaste: this._onPaste,
      [this._keyPressPropName()]: this._onKeyPress
    };
  }
// ...

...And the source is compiled as:

// compiled type signature file
// ...
    _keyPressPropName(): "onBeforeInput" | "onKeyPress";
    _getEventHandlers(): {
        [x: string]: (e: React.ChangeEvent<HTMLInputElement>) => void;
        onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
        onKeyDown: (e: any) => void;
        onPaste: (e: React.ClipboardEvent<any>) => void;
    };

Though _keyPressPropName is Union type, because of typescript issue that union type cannot be a key of object, _getEventHandlers is compiled with x key which is a string type.

This makes compile error Property 'onPaste' of type '(e: ClipboardEvent<any>) => void' is not assignable to string index type '(e: ChangeEvent<HTMLInputElement>) . this should be fixed by more explicit type declaration on method:

  _getEventHandlers(): {
    onChange: (TChangeEvent) => void;
    onKeyDown: (TChangeEvent) => void;
    onPaste: (TClipboardEvent) => void;
    onBeforeInput?: (TChangeEvent) => void;
    onKeyPress?: (TChangeEvent) => void;
  } {
    return {
      onChange: this._onChange,
      onKeyDown: this._onKeyDown,
      onPaste: this._onPaste,
      [this._keyPressPropName()]: this._onKeyPress
    };
  }

which is compiled liked this:

    _getEventHandlers(): {
        onChange: (TChangeEvent: any) => void;
        onKeyDown: (TChangeEvent: any) => void;
        onPaste: (TClipboardEvent: any) => void;
        onBeforeInput?: (TChangeEvent: any) => void;
        onKeyPress?: (TChangeEvent: any) => void;
    };
  • Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. StackOverflow, personal fork, etc.)
antoniopresto added a commit that referenced this issue Jul 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants