Uh oh!
There was an error while loading. Please reload this page.
add compiler flag to enable granular inference outside var/let - #17785
add compiler flag to enable granular inference outside var/let#17785kiara (KiaraGrouwstra) wants to merge 2 commits into
Conversation
Claudia Meadows (dead-claudia)
commented
Aug 15, 2017
Question: does this affect either of theses? If so, you'll need to add a special case to allow them, or you'll need an ungodly number of casts for things like options objects, etc. interfaceSomeInterface{a: boolean}declarefunctionfoo(arg: number[]): voiddeclarefunctionbar(arg: SomeInterface): void// As variable assigneesconsta: number[]=[1,2,3];constb: SomeInterface={a: true};// Same, but as argumentsfoo([1,2,3]);bar({a: true}); |
@isiahmeadows: // @widenTypes: false interfaceSomeInterface{a: boolean}declarefunctionfoo(arg: number[]): voiddeclarefunctionbar(arg: SomeInterface): void// As variable assigneesconsta: number[]=[1,2,3];// number[]constb: SomeInterface={a: true};// SomeInterface// Same, but as argumentsfoo([1,2,3]);// ok, tuple assignable to arraybar({a: true});// ok, assignable
I thought of still typing
I'm kinda neutral there, I presume things will gravitate toward option 1.
Well, Edit: updated. |
4bea1a9 to
601f5f2CompareOkay, got parameters down too. Now, this was dubbed a huge / massive breaking change, so... what's the damage?
That's it; couple dozen casts on a humongous code-base. So the good news is, this doesn't even depend on #17765; it seems good now. I'm ready for feedback. |
7b9bf2c to
c85ba7bCompareTom Crockett (pelotom)
commented
Oct 19, 2017
This looks great! I am constantly annoyed by the fact that TypeScript doesn't always infer the most specific type. Is anyone taking a look at this PR? |
John Nilsson (JohnNilsson)
commented
Oct 22, 2017
I came here looking for a "noImplicitWidening" option to disable all, well, implicit type widening. Is this PR an implementation of that? |
kiara (KiaraGrouwstra)
commented
Oct 22, 2017
John Nilsson (@JohnNilsson): pretty much. The exception here is the right-hand side of (Explicit annotations like |
Tom Crockett (pelotom)
commented
Oct 22, 2017
John Nilsson (@JohnNilsson) same, I just want a flag to disable type widening everywhere. If I ever need to widen a tuple to an array I’m fine with doing that via a manual cast. |
Boris Cherny (bcherny)
commented
Apr 22, 2018
Any updates on this? Is this PR blocked on something, or can we make a yes/no call on whether this can make it in? |
Mohamed Hegazy (mhegazy)
commented
Apr 23, 2018
I do not think this is a direction we are willing to peruse at the time being. First, we really do not want flags that change type system behavior since that effectively creates dialects of the language; the ones we have allowed in the past fall into one of two categories, 1. temporary patches for breaking changes behavior (e.g. Second, overloading the meaning of const is not something we are crazy about. we have done something similar with function parameters, but do not think we want to sanction this meaning of const declarations. Sorry for the delay. |
Tom Crockett (pelotom)
commented
Apr 23, 2018
Mohamed Hegazy (@mhegazy) is the TypeScript team thinking about how to solve the usability issues around type widening? In my mind and I think a lot of others it is the biggest wart on the language currently. |
Mohamed Hegazy (mhegazy)
commented
Apr 23, 2018
Yes, and we continue to think of ways to alleviate these pains. One thing to note is it took us a few iterations to get where we are now. and there are many scenarios on both sides of any change at this point that would break. we try to balance all these as we introduce changes. |
kiara (KiaraGrouwstra)
commented
Apr 23, 2018
Thanks for the response. fwiw, I used the flag figuring this fell under # 2 -- stricter, (imo) preferable, some (imo minor) migration. I'd prefer not needing flags as well. The semantic overloading of |
This is a new compiler flag (idea: #16389 (comment)) to cancel type widening for objects / arrays (and even infer those as tuples) in
constdeclarations as well as parameters:This would fix inference issues that spawned #3369, #6574, #7799, #8276, #9216, #9217, #10195, #12308, #12955, #15656, #16276, #16389, #16523, #16656, #17181. (Probably not exhaustive, just followed some links.)
Some of those propose alternate solutions like new keywords, though I presume this would address their issues as well.
Sometimes one may want granular types, sometimes one may want widened types. And in parameter positions, unfortunately only one of the two can be default, so either will end up wrong in some cases.
For declarations this holds as well, but the appeal of this flag there is that it yields users more control, based on whether they use the
letorconstkeyword.Notes:
checkMode, or even the nodes too) be standardized into some options objects?