-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
"typeof" operator should prevent widening #22211
Comments
A real-world example that would benefit from this change is pelotom/runtypes, which breaks in situations which reduce to the example above. There are certainly other corner cases, such as the example in #16337, which will also unexpectedly break type safety. |
I should have left myself more notes in the linked issue, but IIRC at least one problem is that this would be a substantial breaking change. We have to pick between the proposed change and the existing symmetry of these declarations being identical: let x1 = runtimeA;
let x2: typeof runtimeA = runtimeA Without a strong principled reason to choose one over the other, stare decisis is sort of the order of the day. If you had to convince someone we were correctly breaking their working code that relied on the current behavior, what would your argument be? |
My strong principled reason would be that implicit type widening is inherently unsafe, since it silently and unexpectedly relaxes an invariant. It falls into the category of conveniences like ASI and aggressive type coercion that are actually loaded guns pointed at your foot. (See also: #20195) But I would rather advance a more narrow argument here. :) When an annotated type is widened, the compiler is stripping and violating a constraint explicitly specified by the author. In your example above, it is counter-intuitive that a variable with an explicit type should behave the same as a variable with an implicit type. Rather, one expects that every variable with an explicit type will behave the same as every other variable with an explicit type. To illustrate with a more complete example than above, which of these things is not like the others?
The asymmetry between an implicit type and an explicit (non-
The symmetry between an explicit (non-
However, we're currently stuck with a counter-intuitive symmetry between an implicit type and an explicit (
Unfortunately, practical usage was difficult to research. A quick perusal of GitHub did reveal the following common but subtly-unsafe pattern:
Due to implicit type widening, this will in certain hard-to-predict situations be widened to
|
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
When someone uses
typeof
, it is unlikely that they want a widening type. This unexpectedly breaks type safety in various situations.This was considered previously for all of 16 minutes in #16337. I think it warrants reconsideration.
TypeScript Version: 2.8.0-dev.20180208
Search Terms:
typeof widen widened widening
Code
Expected behavior:
Actual behavior:
Related Issues:
#16337
The text was updated successfully, but these errors were encountered: