From 4b33c658c51753fd9909cbc38d3f2afbb76fab5c Mon Sep 17 00:00:00 2001 From: Prithpal Sooriya Date: Fri, 20 Dec 2024 15:50:03 +0000 Subject: [PATCH] refactor: improve intellisense for selector subscriptions 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. --- packages/base-controller/src/Messenger.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/base-controller/src/Messenger.ts b/packages/base-controller/src/Messenger.ts index 4d10201913..23bb150c4b 100644 --- a/packages/base-controller/src/Messenger.ts +++ b/packages/base-controller/src/Messenger.ts @@ -58,9 +58,12 @@ export type SelectorFunction< EventType extends Event['type'], ReturnValue, > = (...args: ExtractEventPayload) => ReturnValue; -export type SelectorEventHandler = ( - newValue: SelectorReturnValue, - previousValue: SelectorReturnValue | undefined, +export type SelectorEventHandler = < + // Deferring value as implicit types do not get used correctly + DeferredVal = SelectorReturnValue, +>( + newValue: DeferredVal, + previousValue: DeferredVal | undefined, ) => void; export type ActionConstraint = {