Error during implementing before realtime hooks

I implemented beforeChannelJoin hook from here

There are some redline(error) showing just under beforeChannelJoin parameter in register function.
initializer.registerRtBefore(“ChannelJoin”, beforeChannelJoin);
When I hover mouse on it, it shows

let beforeChannelJoin: nkruntime.RtBeforeHookFunction<nkruntime.EnvelopeChannelJoin>
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'.ts(2345)

There are also red line(error) showing under result.friends.filter(function (friend) and friend.user.userId
When I hover I get same error for both of them:

Object is possibly 'undefined'.ts(2532)

main.ts

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;
};