-
Notifications
You must be signed in to change notification settings - Fork 12.8k
typeToString fails to reduce subtypes #16582
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
Comments
The issue here is that subtype reduction has observable effects. It definitely wouldn't be good for |
@ahejlsberg Can you explain that to me in simpler terms? I'm not entirely sure what you mean with "observably different". |
I'm not sure what @ahejlsberg is saying either. I assume that if ts lets me declare the same variable twice with two type annotations, then the two annotated types are not observably different. For example, the lack of error in var s: string;
var s: string | string; // no error witnesses that If you look at var a1 = { a: 1 };
var a2 = { a: 2 };
var x: typeof a1;
var x: typeof a2; // no error
var x: { a: number; }; // no error it sure looks like the types of both the value But var x: typeof a1;
var x: typeof a2; // no error, as before
var x: typeof a1 | typeof a1 // no error
var x: typeof a1 | typeof a2; // error!! gives the error message So what am I missing? Thanks! |
It occurs to me that the real issue is probably that union reduction in the type checker uses reference equality to recognize when to apply the law of idempotence ( Is it too expensive to compare types by value? |
@ahejlsberg's point is that if you take the following: var x = Math.random() < 0.5 ? { a: 100 } : { a: 100, b: 200 }; The type of So instead of subtype reduction, you may want identity reduction. You may be right that we use reference equality for this @jcalz. |
@DanielRosenwasser I'm trying to understand why we don't want subtype reduction to take place. Where does the difference between var x: { a: number } | { a: number, b: number };
x = {a: 100, b: 200}; // no excess property error
x = {a: 100, b: 'two hundred'}; // no error either!
x = {a: 100, b: 'two hundred', c: 3}; // excess property error Is there anything close to a "canonical" discussion around subtype reduction where I can read more? (something like #4537?) Thanks. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
Actual:

Expected:
result1
andresult2
both diplay their type as{ a: number }
Thanks @lhecker for pointing out this problem.
The text was updated successfully, but these errors were encountered: