Skip to content

Chapter 8's RedisClient example #9

Open
@peter-csala

Description

@peter-csala

In chapter 8 (Async Streams >> Event Emitters, on pages 185-186) you have defined the following two types:

type Events = {    
  readyvoid    
  errorError    
  reconnecting{
    delaynumber, 
    attemptnumber
    }
}

type RedisClient = {    
  on<E extends keyof Events>(eventE, f(argsEvents[E]) => void)void
}

I would like to have a naive implementation like this:

let ClientRedisClient = {        
  on<E extends keyof Events>(eventE, f(argsEvents[E]) => void) {
        if(event === "reconnecting") {
             f({delay: 1, attempt: 1})
        }    
    }
}
Client.on("reconnecting", (param :{delay: number, attempt: number}) => console.log(param.delay))

Unfortunately it gives me the following error (at this f({delay: 1, attempt: 1}) line):

Argument of type '{ delay: number; attempt: number; }' is not assignable to parameter of type 'Events[E]'.
  Type '{ delay: number; attempt: number; }' is not assignable to type 'void & Error & { delay: number; attempt: number; }'.
    Type '{ delay: number; attempt: number; }' is not assignable to type 'void'.(2345)

It seems like it uses intersection instead of union. How can I fix this?

I've found a related Typescript issue, but I couldn't manage to apply the suggested workaround in this example. Can you help me with this please?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions