Skip to content

isValidMethodAccess: Instantiate signature this type if necessary #21722

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

Merged
1 commit merged into from
Mar 6, 2018
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
23 changes: 17 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6959,7 +6959,10 @@ namespace ts {
}

function createSignatureInstantiation(signature: Signature, typeArguments: Type[]): Signature {
return instantiateSignature(signature, createTypeMapper(signature.typeParameters, typeArguments), /*eraseTypeParameters*/ true);
return instantiateSignature(signature, createSignatureTypeMapper(signature, typeArguments), /*eraseTypeParameters*/ true);
}
function createSignatureTypeMapper(signature: Signature, typeArguments: Type[]): TypeMapper {
return createTypeMapper(signature.typeParameters, typeArguments);
}

function getErasedSignature(signature: Signature): Signature {
Expand Down Expand Up @@ -11408,8 +11411,8 @@ namespace ts {
}
}

function createInferenceContext(typeParameters: TypeParameter[], signature: Signature, flags: InferenceFlags, compareTypes?: TypeComparer, baseInferences?: InferenceInfo[]): InferenceContext {
const inferences = baseInferences ? map(baseInferences, cloneInferenceInfo) : map(typeParameters, createInferenceInfo);
function createInferenceContext(typeParameters: TypeParameter[], signature: Signature | undefined, flags: InferenceFlags, compareTypes?: TypeComparer, baseInferences?: InferenceInfo[]): InferenceContext {
const inferences = baseInferences ? baseInferences.map(cloneInferenceInfo) : typeParameters.map(createInferenceInfo);
const context = mapper as InferenceContext;
context.typeParameters = typeParameters;
context.signature = signature;
Expand Down Expand Up @@ -16399,15 +16402,23 @@ namespace ts {
return isValidPropertyAccessWithType(node, node.expression, property.escapedName, type)
&& (!(property.flags & SymbolFlags.Method) || isValidMethodAccess(property, type));
}
function isValidMethodAccess(method: Symbol, type: Type) {
function isValidMethodAccess(method: Symbol, actualThisType: Type): boolean {
const propType = getTypeOfFuncClassEnumModule(method);
const signatures = getSignaturesOfType(getNonNullableType(propType), SignatureKind.Call);
Debug.assert(signatures.length !== 0);
return signatures.some(sig => {
const thisType = getThisTypeOfSignature(sig);
return !thisType || isTypeAssignableTo(type, thisType);
const signatureThisType = getThisTypeOfSignature(sig);
return !signatureThisType || isTypeAssignableTo(actualThisType, getInstantiatedSignatureThisType(sig, signatureThisType, actualThisType));
});
}
function getInstantiatedSignatureThisType(sig: Signature, signatureThisType: Type, actualThisType: Type): Type {
if (!sig.typeParameters) {
return signatureThisType;
}
const context = createInferenceContext(sig.typeParameters, sig, InferenceFlags.None);
inferTypes(context.inferences, actualThisType, signatureThisType);
Copy link
Member

Choose a reason for hiding this comment

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

It's a little weird to do limited inference here, but maybe it makes sense because this is called solely for completions? The inference and instantiation will happen later, and lead to an error, in normal compilation.

Copy link
Author

Choose a reason for hiding this comment

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

Since the call expression doesn't actually exist (we're just considering adding the completion, all that exists so far is obj.), this won't have a corresponding normal compilation.

return instantiateType(signatureThisType, createSignatureTypeMapper(sig, getInferredTypes(context)));
}

function isValidPropertyAccessWithType(
node: PropertyAccessExpression | QualifiedName,
Expand Down
6 changes: 4 additions & 2 deletions tests/cases/fourslash/completionsMethodWithThisParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
//// value: T; // Make the type parameter actually matter
//// ms(this: A<string>) {}
//// mo(this: A<{}>) {}
//// mp<P>(this: A<P>) {}
//// mps<P extends string>(this: A<P>) {}
////}
////
////const s = new A<string>();
////const n = new A<number>();
////s./*s*/;
////n./*n*/;

verify.completionsAt("s", ["value", "ms", "mo"]);
verify.completionsAt("n", ["value", "mo"]);
verify.completionsAt("s", ["value", "ms", "mo", "mp", "mps"]);
verify.completionsAt("n", ["value", "mo", "mp"]);