Skip to content

Commit

Permalink
refactor: improve intellisense for selector subscriptions
Browse files Browse the repository at this point in the history
currently if you do not provide explicit type in the selector, then the handler function will have `unknown` params.
This is because TS does not know which generic type to use for `SelectorReturnValue`.

I tried using the `NoInfer` type to prevent the handler function from winning the inference, but unfortunately `NoInfer` does not work with return values.

The other option was to switch the handler fn and selector fn around, however this is a destructive change.

By making the handler fn use its own generics (which are constrained by the original `SelectorReturnValue`), we effectively defer or ensure that the handler doesn't win the inference.
  • Loading branch information
Prithpal-Sooriya committed Dec 20, 2024
1 parent e825794 commit 4b33c65
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/base-controller/src/Messenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ export type SelectorFunction<
EventType extends Event['type'],
ReturnValue,
> = (...args: ExtractEventPayload<Event, EventType>) => ReturnValue;
export type SelectorEventHandler<SelectorReturnValue> = (
newValue: SelectorReturnValue,
previousValue: SelectorReturnValue | undefined,
export type SelectorEventHandler<SelectorReturnValue> = <
// Deferring value as implicit types do not get used correctly
DeferredVal = SelectorReturnValue,
>(
newValue: DeferredVal,
previousValue: DeferredVal | undefined,
) => void;

export type ActionConstraint = {
Expand Down

0 comments on commit 4b33c65

Please sign in to comment.