Hacker News new | ask | show | jobs
by gernb 1460 days ago
I think I disagree (though happy to be corrected). It's common to have dervied types (example Base = Shape, Derived = Circle, Rectangle, Hexagon). Often you have a collection of Shape and often there is some runtime code that can go from a Base type to a Dervied type based on some key or value but the relationship between the key and the Dervied type is rarely directly expressed in the type system where as in typescript it can be.

I think an example is how typescript can know, based on the first argument to a listener, what the Event type coming in will be

    elem.addEventListner('mousedown', (foo) => {...});
    elem.addEventListner('keydown', (bar) => {...});
typescript knows foo is a MouseEvent and bar is a KeyboardEvent

Of course you could argue that `addEventListener` is just bad design but I feel like there are legit uses to being able to associate an enum or string with type and I haven't seen that feature in other languages I've used.

1 comments

Keeping in mind that it can only associate compile-time-known values, there's no practical advantage over something like a tagged union.