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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This PR improves type inference involving source and target sides that are both union or intersection types. When inferring from a type S to a type T, if S and T are both union types or both intersection types, we first reduce S and T by removing constituents that are matched by a constituent in the other type. For example, when inferring from string | string[] to string | T, we reduce the types to string[] and T, thus inferring string[] for T.
An example:
typeMaybe<T>=T|void;functionisDefined<T>(x: Maybe<T>): x is T{returnx!==undefined&&x!==null;}functionisUndefined<T>(x: Maybe<T>): x is void{returnx===undefined||x===null;}functiongetOrElse<T>(x: Maybe<T>,defaultValue: T): T{returnisDefined(x) ? x : defaultValue;}functiontest1(x: Maybe<string>){letx1=getOrElse(x,"Undefined");// stringletx2=isDefined(x) ? x : "Undefined";// stringletx3=isUndefined(x) ? "Undefined" : x;// string}functiontest2(x: Maybe<number>){letx1=getOrElse(x,-1);// numberletx2=isDefined(x) ? x : -1;// numberletx3=isUndefined(x) ? -1 : x;// number}
@ahejlsberg You've got a little typo in your code example: isUndefined<T> should use or rather than and, shouldn't it? Just trying to avoid confusion here.
functionisUndefined<T>(x: Maybe<T>): x is void{returnx===undefined||x===null;}
The type of result is inferred as {}, although I would expect it to be inferred as {b: number}. Admittedly I don't have a very clear use case for this at the moment.
@masaeedu We could potentially explore that, but it could get complex and would require type inference to manufacture new types, neither of which I'm crazy about. Specifically, in your example, we'd have to manufacture a new type { b: number } and infer that for T. I think it is desirable to have the property that type inference never infers types that don't already occur in your code.
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
None yet
8 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR improves type inference involving source and target sides that are both union or intersection types. When inferring from a type S to a type T, if S and T are both union types or both intersection types, we first reduce S and T by removing constituents that are matched by a constituent in the other type. For example, when inferring from
string | string[]
tostring | T
, we reduce the types tostring[]
andT
, thus inferringstring[]
forT
.An example:
Fixes #2264.
Fixes #4212.
Fixes #5417.
Fixes #5456.