News for hackers. Stuff that matters.
Re:TypeScript – The Myth of the Superset by k__ on Sunday October 02, 2016 @12:36PM (#12623106)
Polymorphism of JavaScript functions is a bit of a pain point in TypeScript, I think.

I often got some query methods, that deliver different types of objects, which share a parent class. Like GameObject, Player, Enemy.

    const player = getGameObject('playerId')
Now player will be typed like the return type of getGameObject. If I now try to set an attribute, specific to `Player`, it will fail:

    player.mousePosition = [1, 2] 
    // won't work, because GameObject doesn't have a mousePosition
Also, setting the type of player to Player, will fail too:

   const player : Player = getGameObject('playerId')
   // getGameObject could return an Enemy
So I have to tell TypeScript, that I know better

   const player = getGameObject('playerId') as Player
   // Forces TypeScript to treat the return value as Player
Re:TypeScript – The Myth of the Superset by BoorishBears on Sunday October 02, 2016 @01:52PM (#12623448)
Why is that last line a problem at all?
Re:TypeScript – The Myth of the Superset by k__ on Sunday October 02, 2016 @04:29PM (#12624068)
It implies that TS doesn't fully understand JS.
Re:TypeScript – The Myth of the Superset by BoorishBears on Sunday October 02, 2016 @05:02PM (#12624225)
I wouldn't say that, that line is pretty much the main value proposition of the language though, is it really "not understanding JS" as much as extending it?

If you really want it to understand Js you could just cast to Any, but to me it's huge that you can do what you do in that last line. Instead of guessing that Player has a mousePosition property, after casting you get told it has one, and get it's exact type.

Re:TypeScript – The Myth of the Superset by k__ on Sunday October 02, 2016 @05:29PM (#12624344)
To me it seemed the other way around. By casting, I'm telling the compiler that it has a mousePosition property.

But I think you're right. This is an simple example and the Player object could have a huge bunch of properties I don't need to guess later, I just need to cast and be done with it.