Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 8's RedisClient example #9

Open
peter-csala opened this issue Oct 26, 2020 · 0 comments
Open

Chapter 8's RedisClient example #9

peter-csala opened this issue Oct 26, 2020 · 0 comments

Comments

@peter-csala
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant