'type myEventHandler<T extends myEvent> = (event: T) => void;
I am new to typescript.
Wondering what exactly this line means in TS?:
type myEventHandler<T extends myEvent> = (event: T) => void;
I know that <> is a type assertion. So I guess myEventHandler should be the type of T. But the 'myEventHandler' is also a type, right? So is that mean we can define a type that belongs to another type?? I guess the event:T is a parameter, so a type can also have a parameter? This line looks like a fat arrow function... and it has a void. Can someone explain it?
Solution 1:[1]
In this case <T extends myEvent> is not a type assertion. It rather declares the type as generic. You can now pass arguments of any type T, as long as the type extends myEvent.
You should read the TypeScript introduction to Generics for further reference.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tobias S. |
