-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
Improve optionality detection in mapped type indexed access substitutions #57946
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7f0590d
Examine resolved members to detect optionality
ahejlsberg 2ce5121
Add regression test
ahejlsberg 6167709
Less aggressive optionality analysis for intersections
ahejlsberg d6a8a25
Additional test
ahejlsberg 609d3f7
Fix formatting
ahejlsberg bb9a3e7
Merge branch 'main' into fix57860
ahejlsberg a2790b5
Require uniformity in intersection modifiers
ahejlsberg 144e560
Merge branch 'main' into fix57860
ahejlsberg 1b8bacb
Accept new baselines
ahejlsberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14115,7 +14115,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
} | ||
|
||
function getModifiersTypeOptionality(type: Type): number { | ||
return type.flags & TypeFlags.Intersection ? Math.max(...map((type as IntersectionType).types, getModifiersTypeOptionality)) : | ||
return type.flags & TypeFlags.Intersection ? Math.min(...map((type as IntersectionType).types, getModifiersTypeOptionality)) : | ||
getObjectFlags(type) & ObjectFlags.Mapped ? getCombinedMappedTypeOptionality(type as MappedType) : | ||
0; | ||
} | ||
|
@@ -18635,11 +18635,26 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
return !!(getUnionType([intersectTypes(type1, type2), neverType]).flags & TypeFlags.Never); | ||
} | ||
|
||
// Given an indexed access on a mapped type of the form { [P in K]: E }[X], return an instantiation of E where P is | ||
// replaced with X. Since this simplification doesn't account for mapped type modifiers, add 'undefined' to the | ||
// resulting type if the mapped type includes a '?' modifier or if the modifiers type indicates that some properties | ||
// are optional. If the modifiers type is generic, conservatively estimate optionality by recursively looking for | ||
// mapped types that include '?' modifiers. | ||
function substituteIndexedMappedType(objectType: MappedType, index: Type) { | ||
const mapper = createTypeMapper([getTypeParameterFromMappedType(objectType)], [index]); | ||
const templateMapper = combineTypeMappers(objectType.mapper, mapper); | ||
const instantiatedTemplateType = instantiateType(getTemplateTypeFromMappedType(objectType.target as MappedType || objectType), templateMapper); | ||
return addOptionality(instantiatedTemplateType, /*isProperty*/ true, getCombinedMappedTypeOptionality(objectType) > 0); | ||
const isOptional = getMappedTypeOptionality(objectType) > 0 || (isGenericType(objectType) ? | ||
getModifiersTypeOptionality(getModifiersTypeFromMappedType(objectType)) > 0 : | ||
couldAccessOptionalProperty(objectType, index)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When processing a substitution for |
||
return addOptionality(instantiatedTemplateType, /*isProperty*/ true, isOptional); | ||
} | ||
|
||
// Return true if an indexed access with the given object and index types could access an optional property. | ||
function couldAccessOptionalProperty(objectType: Type, indexType: Type) { | ||
const indexConstraint = getBaseConstraintOfType(indexType); | ||
return !!indexConstraint && some(getPropertiesOfType(objectType), p => !!(p.flags & SymbolFlags.Optional) && | ||
isTypeAssignableTo(getLiteralTypeFromProperty(p, TypeFlags.StringOrNumberLiteralOrUnique), indexConstraint)); | ||
} | ||
|
||
function getIndexedAccessType(objectType: Type, indexType: Type, accessFlags = AccessFlags.None, accessNode?: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression, aliasSymbol?: Symbol, aliasTypeArguments?: readonly Type[]): Type { | ||
|
This file contains 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 file contains 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 file contains 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 file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only claim that intersections add optionality when all constituents do so. Previously, optionality in any constituent would cause us to claim optionality for the entire intersection, but that is too aggressive.