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.
Resolves #15397
Worked on this as an alternative way to address #15114 (instead of #15398). This PR does two things:
true ? 'a' : 'b'
is'a'
, and the type offalse ? 'a' : 'b'
is'b'
. There was a TODO comment in TypeAssignmentVisitor suggesting this change.unknownCondition ? 'a' : 'b'
is'a' | 'b'
(a union), but the type ofunknownCondition ? [stringParam] : [intParam]
is[int | string]
(assuming the type ofstringParam
isstring
and the type ofintParam
isint
). This change relies on existing type collapsing logic, so it will handle things like combining refinements on string types and combining objects into tagged unions if possible.One change I made to the TypeCollapser is to collapse objects that can't be combined into a tagged union into an object whose properties are a union of the possible property types of the inputs. This is similar to how we collapse tuple types. Given a template like the following:
value
would have a type of{ bar: string, foo: int | string, *: int }
, with all properties flagged as optional.Microsoft Reviewers: Open in CodeFlow