Hacker News new | ask | show | jobs
by xg15 1544 days ago
For consts and function arguments yes, I think. But you could have some mutable variable or field whose value depends on runtime state. In that case, you could have an actual polymorphic type.
1 comments

But the previous example isn't that case.

if (c.type === 'a') { /* c is of type A here */ }

This is dynamic typing, this code is checked at runtime, and it's leveraged statically.

Yes, that's correct. In the GP example, c was const, so the type can be determined at compile time.
I was writing TypeScript, where const just means the variable is not reassigned. For instance this is valid:

    const c: C = Math.random() < 0.5 ? { type: 'a' } : { type: 'b' }.
Ah, I'm sorry. You're right of course!