Skip to content

Reexport InferEventType in index.ts #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

emmiep
Copy link

@emmiep emmiep commented Jan 16, 2025

When using bind in helper functions/libraries/etc it's necessary to use the InferEventType to get correct event types for the target.
For instance, I want to create a React hook to add global event listeners to window, document etc:

function SomeReactComponent() {
  useGlobalEventListener(window, "focus", (event) => {
    console.log("Event:", event);
  }, []);

  // …
};

function useGlobalEventListener<
  TTarget extends EventTarget,
  TType extends InferEventType<TTarget>,
>(
  target: TTarget,
  type: TType,
  listener: Listener<TTarget, TType>,
  deps: DependencyList
) {
  useEffect(() => {
    if (!target) {
      return;
    }

    const unbind = bind(target, { type, listener });

    return () => {
      unbind();
    };
  }, [target, type, ...deps]);
}

However, since InferEventType isn't reexported from index.ts you have to do a bit of a hackish import from src/types.ts instead:

import type { InferEventType, Listener } from "bind-event-listener/src/types";

This doesn't give me any file path completion in vscode, so you need to know the exact source of the file with the typings.
I think it makes sense to reexport InferEventType from index.ts for these kinds of use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant