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
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
// ok
new Subject
// not allowed
as opposed to non-void where you get
new Subject
Not sure exactly which TS version this became possible.