3 ms·
The use case is having types depend on control flow so that guarded control flow paths don't need further typecasting. I don't see how this is bonkers. It looks
by barrkel 8y ago
The use case is having types depend on control flow so that guarded control flow paths don't need further typecasting. I don't see how this is bonkers. It looks like a great ergonomic feature to me.
As to your code, sure, it's an error. But it's not unusual, conceptually. For example:
if (foo) {
int bar = 22;
}
print(bar); // compilation error??
And this:
if (foo) {
print(bar); // compilation error??
int bar = 22;
}
More idiomatically, you'd expect to read code like this:
if (isWizard(person)) { person.wizardStuff(); }
I think this is not an unusual thing to do in dynamically typed languages, especially when interfacing with third-party code where you can't easily add new polymorphic methods (so you can't rely on virtual dispatch to handle your if-case logic). Making it work more ergonomically in typescript isn't "absolutely bonkers".