# Converting RT hook functions to generics for type support

**URL:** <https://forum.heroiclabs.com/t/converting-rt-hook-functions-to-generics-for-type-support/4194>\
**Category:** Runtime Framework\
**Tags:** server-framework, typescript\
**Created:** [August 5, 2023, 6:49am UTC](https://forum.heroiclabs.com/t/converting-rt-hook-functions-to-generics-for-type-support/4194 "2023-08-05T06:49:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noita](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/noita/32/1747_2.png) [@Noita](https://forum.heroiclabs.com/u/Noita)\
**Post date:** [August 5, 2023, 6:49am UTC](https://forum.heroiclabs.com/t/converting-rt-hook-functions-to-generics-for-type-support/4194/1 "2023-08-05T06:49:39Z")

</div>

Hello!  
I figured it’d be interesting to see a conversion of the typedefs for the functions `registerRtBefore` and `registerRtAfter`

The issue I encountered was typing hook callbacks specifically to fit the Realtime channel I wanted to hook onto. The code [from the docs](https://heroiclabs.com/docs/nakama/guides/server-framework/using-hooks/#after-channel-leave) for example throws a type error without narrowing the types upstream.

My proposal is to convert the realtime functions to generics as to infer the types based on the `RtBeforeHookFunction` type or the type passed to the generic on the hook functions themselves. Please see below!

```ts
/**
         * Register a hook function to be run before an RPC function is invoked.
         * The RPC call is identified by the id param.
         *
         * @param id - The ID of the RPC function.
         * @param func - The Hook function logic to execute before the RPC is called.
         */
        registerRtBefore<T extends Envelope>(id: RtHookMessage, func: RtBeforeHookFunction<T>): void;

        /**
         * Register a hook function to be run after an RPC function is invoked.
         * The RPC call is identified by the id param.
         *
         * @param id - The ID of the RPC function.
         * @param func - The Hook function logic to execute after the RPC is called.
         */
        registerRtAfter<T extends Envelope>(id: RtHookMessage, func: RtAfterHookFunction<T>): void;

```

I suppose the second stage to this would be typeguards on `RtHookMessage` that narrow the rest of the function as well haha.

Please let me know what you think! Thanks all 🙂

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [August 8, 2023, 1:29pm UTC](https://forum.heroiclabs.com/t/converting-rt-hook-functions-to-generics-for-type-support/4194/2 "2023-08-08T13:29:05Z")

</div>

Hello @Noita,

I think is is a good suggestion, would you like to contribute by opening a PR at [GitHub - heroiclabs/nakama-common: The runtime framework for Nakama server.](https://github.com/heroiclabs/nakama-common)?

Best.

---

<div class="post-metadata">

**Author:** ![Noita](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/noita/32/1747_2.png) [@Noita](https://forum.heroiclabs.com/u/Noita)\
**Post date:** [August 9, 2023, 12:53am UTC](https://forum.heroiclabs.com/t/converting-rt-hook-functions-to-generics-for-type-support/4194/3 "2023-08-09T00:53:19Z")

</div>

Sure thing! I’ll see about making a PR!  
PR submitted [Genericize realtime before and after hooks by Noitabara · Pull Request #127 · heroiclabs/nakama-common · GitHub](https://github.com/heroiclabs/nakama-common/pull/127)
