Skip to content
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

🤖 Pick PR #53207 (Fixed symbol declarations for gener...) into release-5.0 #53271

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13179,9 +13179,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);
Comment on lines +13183 to +13184
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wasn't nameType just passed in?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to balance the reusability of this helper since it’s now used in two (?) places and i have a PR that wants to use it in another one: 10a9bb6

Not every call site has independent access to this

const templateType = getTemplateTypeFromMappedType(mappedType);
const modifiersType = getApparentType(getModifiersTypeFromMappedType(type)); // The 'T' in 'keyof T'
const templateModifiers = getMappedTypeModifiers(type);
const include = keyofStringsOnly ? TypeFlags.StringLiteral : TypeFlags.StringOrNumberLiteralOrUnique;
Expand Down Expand Up @@ -13227,7 +13228,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);
}
Expand Down Expand Up @@ -13360,6 +13361,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) {
Expand Down Expand Up @@ -17478,8 +17484,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));
}
}
Expand Down
25 changes: 25 additions & 0 deletions tests/cases/fourslash/goToDefinition_filteringGenericMappedType.ts
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");