logoalt Hacker News

culitoday at 12:23 AM2 repliesview on HN

You don't even need that. The code exactly as presented acts as a discriminator. TypeScript is smart enough to handle that logic in the if block and know whether animal has been validated as Dog vs Cat. GP is complaining about a feature that already exists in TypeScript


Replies

JoeyJoJoJrtoday at 6:45 AM

I’m not at my computer so I can’t remember the exact behavior of this situation, but was OP more so referring to autocomplete abilities of typescript? I think they were saying, you first must know if the object barks or meows, you must first type that in in order to get the benefit of type checking and subsequent autocomplete conditional body, which is annoying when you are dealing with complicated types. It requires you to do some hunting in to the types, rather than using a piece of code more like an interface.

littlecranky67today at 12:28 AM

It depends how you construct Dog and Cat. With Javascripts dynamic prototype chain, you could never know for sure.

show 1 reply