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

type error after compile #4

@palindrom615

Description

@palindrom615
  • 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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions