-
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
Fixed symbol declarations for generic filtering mapped types #53207
Merged
weswigham
merged 2 commits into
microsoft:main
from
Andarist:fix/generic-filtered-mapped-types-declarations
Mar 15, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -13174,9 +13174,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
// and T as the template type. | ||
const typeParameter = getTypeParameterFromMappedType(type); | ||
const constraintType = getConstraintTypeFromMappedType(type); | ||
const nameType = getNameTypeFromMappedType(type.target as MappedType || type); | ||
const isFilteringMappedType = nameType && isTypeAssignableTo(nameType, typeParameter); | ||
const templateType = getTemplateTypeFromMappedType(type.target as MappedType || type); | ||
const mappedType = (type.target as MappedType) || type; | ||
const nameType = getNameTypeFromMappedType(mappedType); | ||
const shouldLinkPropDeclarations = !nameType || isFilteringMappedType(mappedType); | ||
const templateType = getTemplateTypeFromMappedType(mappedType); | ||
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. This line should also just reference |
||
const modifiersType = getApparentType(getModifiersTypeFromMappedType(type)); // The 'T' in 'keyof T' | ||
const templateModifiers = getMappedTypeModifiers(type); | ||
const include = keyofStringsOnly ? TypeFlags.StringLiteral : TypeFlags.StringOrNumberLiteralOrUnique; | ||
|
@@ -13222,7 +13223,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
prop.links.keyType = keyType; | ||
if (modifiersProp) { | ||
prop.links.syntheticOrigin = modifiersProp; | ||
prop.declarations = !nameType || isFilteringMappedType ? modifiersProp.declarations : undefined; | ||
prop.declarations = shouldLinkPropDeclarations ? modifiersProp.declarations : undefined; | ||
} | ||
members.set(propName, prop); | ||
} | ||
|
@@ -13355,6 +13356,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
return false; | ||
} | ||
|
||
function isFilteringMappedType(type: MappedType): boolean { | ||
const nameType = getNameTypeFromMappedType(type); | ||
return !!nameType && isTypeAssignableTo(nameType, getTypeParameterFromMappedType(type)); | ||
} | ||
|
||
function resolveStructuredTypeMembers(type: StructuredType): ResolvedType { | ||
if (!(type as ResolvedType).members) { | ||
if (type.flags & TypeFlags.Object) { | ||
|
@@ -17473,8 +17479,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
// K is generic and N is assignable to P, instantiate E using a mapper that substitutes the index type for P. | ||
// For example, for an index access { [P in K]: Box<T[P]> }[X], we construct the type Box<T[X]>. | ||
if (isGenericMappedType(objectType)) { | ||
const nameType = getNameTypeFromMappedType(objectType); | ||
if (!nameType || isTypeAssignableTo(nameType, getTypeParameterFromMappedType(objectType))) { | ||
if (!getNameTypeFromMappedType(objectType) || isFilteringMappedType(objectType)) { | ||
return type[cache] = mapType(substituteIndexedMappedType(objectType, type.indexType), t => getSimplifiedType(t, writing)); | ||
} | ||
} | ||
|
25 changes: 25 additions & 0 deletions
25
tests/cases/fourslash/goToDefinition_filteringGenericMappedType.ts
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
///<reference path="fourslash.ts"/> | ||
|
||
//// const obj = { | ||
//// get /*def*/id() { | ||
//// return 1; | ||
//// }, | ||
//// name: "test", | ||
//// }; | ||
//// | ||
//// type Omit2<T, DroppedKeys extends PropertyKey> = { | ||
//// [K in keyof T as Exclude<K, DroppedKeys>]: T[K]; | ||
//// }; | ||
//// | ||
//// declare function omit2<O, Mask extends { [K in keyof O]?: true }>( | ||
//// obj: O, | ||
//// mask: Mask | ||
//// ): Omit2<O, keyof Mask>; | ||
//// | ||
//// const obj2 = omit2(obj, { | ||
//// name: true, | ||
//// }); | ||
//// | ||
//// obj2.[|/*ref*/id|]; | ||
|
||
verify.goToDefinition("ref", "def"); |
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.
the bug was here, this likely should have been something like this:
and this is what happens in the added
isFilteringMappedType
since it unpacks the correct mapped type on its own and uses it as the source of information for both arguments passed toisTypeAssignableTo