-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
add compiler flag to enable granular inference outside var/let #17785
add compiler flag to enable granular inference outside var/let #17785
Conversation
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. interface SomeInterface { a: boolean }
declare function foo(arg: number[]): void
declare function bar(arg: SomeInterface): void
// As variable assignees
const a: number[] = [1, 2, 3];
const b: SomeInterface = {a: true};
// Same, but as arguments
foo([1, 2, 3]);
bar({a: true}); |
@isiahmeadows: // @widenTypes: false
interface SomeInterface { a: boolean }
declare function foo(arg: number[]): void
declare function bar(arg: SomeInterface): void
// As variable assignees
const a: number[] = [1, 2, 3]; // number[]
const b: SomeInterface = {a: true}; // SomeInterface
// Same, but as arguments
foo([1, 2, 3]); // ok, tuple assignable to array
bar({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
601f5f2
Compare
Okay, 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
c85ba7b
Compare
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? |
I came here looking for a "noImplicitWidening" option to disable all, well, implicit type widening. Is this PR an implementation of that? |
@JohnNilsson: pretty much. The exception here is the right-hand side of (Explicit annotations like |
@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. |
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? |
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. |
@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. |
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. |
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
const
declarations 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
let
orconst
keyword.Notes:
checkMode
, or even the nodes too) be standardized into some options objects?