Skip to content

Commit

Permalink
Add correct this type for event handlers (#2166)
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Dec 3, 2019
1 parent 51f0fb8 commit b40d02c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/jsx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,11 @@ export namespace JSXInternal {
>;

interface EventHandler<E extends TargetedEvent> {
(event: E): void;
/**
* The `this` keyword always points to the DOM element the event handler
* was invoked on. See: https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Event_handlers#Event_handlers_parameters_this_binding_and_the_return_value
*/
(this: E['currentTarget'], event: E): void;
}

type AnimationEventHandler<Target extends EventTarget> = EventHandler<
Expand Down Expand Up @@ -415,7 +419,7 @@ export namespace JSXInternal {

// Details Events
onToggle?: GenericEventHandler<Target>;

// Focus Events
onFocus?: FocusEventHandler<Target>;
onFocusCapture?: FocusEventHandler<Target>;
Expand Down
6 changes: 6 additions & 0 deletions test/ts/Component-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import {
Fragment
} from '../../src/';

// Test `this` binding on event handlers
function onHandler(this: HTMLInputElement, event: any) {
return this.value;
}
const foo = <input onChange={onHandler} />;

export class ContextComponent extends Component<{ foo: string }> {
getChildContext() {
return { something: 2 };
Expand Down

0 comments on commit b40d02c

Please sign in to comment.