-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ternary type inference is wrong #16870
Comments
TypeScript uses a structural type system. To TypeScript, an instance of Here, there is a structural difference between the two types, so you get what you expect: class Base { }
class Derived1 extends Base {
foo = 'bar'
}
class Derived2 extends Base {
bar = 1
}
var t1 = (Math.random() < 0.5) ? "asdasd" : 34;
// t1: string | number
var t2 = (Math.random() < 0.5) ? new Derived1() : new Derived2();
// t2: Derived1 | Derived 2 JavaScript behaves in a structural way in everything except the |
really? |
Please read the FAQ https://github.com/Microsoft/TypeScript/wiki/FAQ |
And for the record, those are pointers to functions that ermmm have the same structure... This is, I am afraid, basic TypeScript concepts. |
TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)
Code
Expected behavior:
I understand the shapes are the same, but this means the runtime types don't match up with the static type.
The text was updated successfully, but these errors were encountered: