DEV Community

Discussion on: Type | Treat 2021 - Wrap-up

Collapse
 
brunnerh profile image
brunnerh

Did not know that in could be used on unions; that is very convenient!

My solution was a bit more verbose due to requiring a type narrowing on the callback argument, but it works as well:

function handleSale(events: {
  [genre in Books['genre'] as `on${genre}`]?:
    (e: Books & { genre: genre }) => void
}) { /* ... */ }
Enter fullscreen mode Exit fullscreen mode