void as a type of an argument of a generic function in TypeScript

前端 未结 7 1292
遥遥无期
遥遥无期 2021-01-11 09:37

It is possible to specify void as a type parameter for generic function in TypeScript. And it works fine for return values of functions. However when void

7条回答
  •  粉色の甜心
    2021-01-11 10:00

    A useful use for void is when you want to make a parameter optional. Angular uses this a lot with RxJS Subject when emitting events and there's no useful type for the event.

    The signature for Subject's next function is :

    next(value?: T)

    You can create a Subject which makes the value parameter forbidden:

    new Subject().next() // ok

    new Subject().next(true) // not allowed

    as opposed to non-void where you get

    new Subject().next(true)

    Not sure exactly which TS version this became possible.

提交回复
热议问题