You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
declarefunctionf<T>(): T;// Inference from binding patterns makes suspicious code like this work.const{ foo }=f();// T: { foo: any }// Motivationdeclarefunctionpick<T,KextendskeyofT>(keys: K[],obj?: T): Pick<T,K>;const_=pick(["b"],{a: "a",b: "b",});// T = "b"const{}=pick(["b"],{a: "a",b: "b",});// T = "b" | "a" (!?) (before fix)
Arguably, these anys should be implicitanys that get an error under noImplicitAny.
Inference from return types is generally suspicious, but binding patterns are kind of in the same domain.
Idea last time: binding patterns are useful for tuples, but not objects.
Given someUnknown: unknown, typeof someUnknown === "object today narrows to object | null, but it doesn't narrow a T extends unknown
Added some changes to do this, then added some changes so keyof NonNullable<T> → keyof T succeeds.
Needed this when you index into these types, you need to preserve new behavior on higher order NonNullable<T> that you get (rather than on the previous T).
Have a parallel effort to improve how intersections with {} operate - could unlock the same scenarios.
Ideally, NonNullable<T> would just be T & {}, especially because intersections compose way better than conditional types.
With more aggressive subtype supertype reduction with {}, we can get there.
Also, need to be able to allow unknown to be split between object, null, undefined, and {} and remerged in CFA.
Can we do this without more aggressive subtype reduction?
Actually, this isn't doing more aggressive subtype reduction, it's just supertype reduction.
[[Above notes now reflect this]]
Two approaches here, one focuses on the negative cases, the other on the positive cases.
Both apply new narrowing
Each PR has different "fallout", and has mitigations for the fallout.
Conclusion: See if we can smush these changes together and take the best of each?
The text was updated successfully, but these errors were encountered:
Limiting Type Argument Inference from Binding Patterns
#49086
Arguably, these
any
s should be implicitany
s that get an error undernoImplicitAny
.Inference from return types is generally suspicious, but binding patterns are kind of in the same domain.
Idea last time: binding patterns are useful for tuples, but not objects.
But then
So when does an object binding pattern provide a useful contextual type?
If the binding pattern has a default.
For literals it seems useless.
More complex defaults?
Is this another level of inference priority?
Conclusion: do it, get reviewed
Narrowing Type Parameters (un)Constrained to
unknown
#49091
#49119
someUnknown: unknown
,typeof someUnknown === "object
today narrows toobject | null
, but it doesn't narrow aT extends unknown
keyof NonNullable<T>
→keyof T
succeeds.NonNullable<T>
that you get (rather than on the previousT
).{}
operate - could unlock the same scenarios.NonNullable<T>
would just beT & {}
, especially because intersections compose way better than conditional types.subtypesupertype reduction with{}
, we can get there.unknown
to be split betweenobject
,null
,undefined
, and{}
and remerged in CFA.The text was updated successfully, but these errors were encountered: