# Compile error in realtime hooks in typescript runtime server

**URL:** <https://forum.heroiclabs.com/t/compile-error-in-realtime-hooks-in-typescript-runtime-server/3230>\
**Category:** Runtime Framework\
**Tags:** server-framework, typescript\
**Created:** [November 3, 2022, 12:25pm UTC](https://forum.heroiclabs.com/t/compile-error-in-realtime-hooks-in-typescript-runtime-server/3230 "2022-11-03T12:25:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shahariar\_Ashik](https://avatars.discourse-cdn.com/v4/letter/s/d78d45/32.png) [@Shahariar\_Ashik](https://forum.heroiclabs.com/u/Shahariar_Ashik)\
**Post date:** [November 3, 2022, 12:25pm UTC](https://forum.heroiclabs.com/t/compile-error-in-realtime-hooks-in-typescript-runtime-server/3230/1 "2022-11-03T12:25:38Z")

</div>

Whenever adding a realtime hooks it is showing error same types of error. When adding MatchmakerAdd hook, it is showing as :

```auto
Argument of type 'RtBeforeHookFunction<EnvelopeMatchmakerAdd>' is not assignable to parameter of type 'RtBeforeHookFunction<Envelope>

```

Whenver adding ChannelJoin hook, it is showing as :

```auto
Argument of type 'RtBeforeHookFunction<EnvelopeChannelJoin>' is not assignable to parameter of type 'RtBeforeHookFunction<Envelope>'.
  Types of parameters 'envelope' and 'envelope' are incompatible.
    Type 'Envelope' is not assignable to type 'EnvelopeChannelJoin'.
      Type 'EnvelopeChannel' is not assignable to type 'EnvelopeChannelJoin'.

```

main.ts

```auto
let InitModule: nkruntime.InitModule = function (
  ctx: nkruntime.Context,
  logger: nkruntime.Logger,
  nk: nkruntime.Nakama,
  initializer: nkruntime.Initializer
) {
  initializer.registerRtBefore("ChannelJoin", beforeChannelJoin);
};
let beforeChannelJoin: nkruntime.RtBeforeHookFunction<nkruntime.EnvelopeChannelJoin> = function (
  ctx: nkruntime.Context,
  logger: nkruntime.Logger,
  nk: nkruntime.Nakama,
  envelope: nkruntime.EnvelopeChannelJoin
): nkruntime.EnvelopeChannelJoin | void {
  // If the channel join is a DirectMessage type, check to see if the user is friends with the recipient first
  if (envelope.channelJoin.type == nkruntime.ChanType.DirectMessage) {
    const result = nk.friendsList(ctx.userId, null, 0, null);
    const filtered = result.friends.filter(function (friend) {
      return friend.user.userId == envelope.channelJoin.target;
    });

    if (filtered.length == 0) {
      throw new Error(
        "You cannot direct message someone you are not friends with."
      );
    }
  }

  return envelope;
};

```

---

<div class="post-metadata">

**Author:** ![muhammedf](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/muhammedf/32/1501_2.png) [@muhammedf](https://forum.heroiclabs.com/u/muhammedf)\
**Post date:** [January 15, 2023, 11:54am UTC](https://forum.heroiclabs.com/t/compile-error-in-realtime-hooks-in-typescript-runtime-server/3230/2 "2023-01-15T11:54:43Z")

</div>

Same thing happened to me. I think its an issue about type covariance.  
We still can use the same function with Envelope type parameter. In function body we can cast it to the desired type.

```auto
let beforeChannelJoin: nkruntime.RtBeforeHookFunction<nkruntime.Envelope> = function (
  ctx: nkruntime.Context,
  logger: nkruntime.Logger,
  nk: nkruntime.Nakama,
  envelope: nkruntime.Envelope
): nkruntime.Envelope| void
{
    let channelJoinEvn = envelope as nkruntime.EnvelopeChannelJoin
    /*some logic*/
    return channelJoinEvn
}

```
