mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 16:38:05 -06:00
fix(46563): show completions at this type (#46581)
This commit is contained in:
parent
c5d9200ec6
commit
407edc95c0
@ -432,6 +432,7 @@ namespace ts {
|
||||
getIndexInfosOfType,
|
||||
getSignaturesOfType,
|
||||
getIndexTypeOfType: (type, kind) => getIndexTypeOfType(type, kind === IndexKind.String ? stringType : numberType),
|
||||
getIndexType: type => getIndexType(type),
|
||||
getBaseTypes,
|
||||
getBaseTypeOfLiteralType,
|
||||
getWidenedType,
|
||||
@ -15344,10 +15345,6 @@ namespace ts {
|
||||
(type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.Index | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) && !isPatternLiteralType(type) ? ObjectFlags.IsGenericIndexType : 0);
|
||||
}
|
||||
|
||||
function isThisTypeParameter(type: Type): boolean {
|
||||
return !!(type.flags & TypeFlags.TypeParameter && (type as TypeParameter).isThisType);
|
||||
}
|
||||
|
||||
function getSimplifiedType(type: Type, writing: boolean): Type {
|
||||
return type.flags & TypeFlags.IndexedAccess ? getSimplifiedIndexedAccessType(type as IndexedAccessType, writing) :
|
||||
type.flags & TypeFlags.Conditional ? getSimplifiedConditionalType(type as ConditionalType, writing) :
|
||||
|
||||
@ -4162,6 +4162,7 @@ namespace ts {
|
||||
getIndexInfosOfType(type: Type): readonly IndexInfo[];
|
||||
getSignaturesOfType(type: Type, kind: SignatureKind): readonly Signature[];
|
||||
getIndexTypeOfType(type: Type, kind: IndexKind): Type | undefined;
|
||||
/* @internal */ getIndexType(type: Type): Type;
|
||||
getBaseTypes(type: InterfaceType): BaseType[];
|
||||
getBaseTypeOfLiteralType(type: Type): Type;
|
||||
getWidenedType(type: Type): Type;
|
||||
|
||||
@ -7414,4 +7414,8 @@ namespace ts {
|
||||
export function escapeSnippetText(text: string): string {
|
||||
return text.replace(/\$/gm, "\\$");
|
||||
}
|
||||
|
||||
export function isThisTypeParameter(type: Type): boolean {
|
||||
return !!(type.flags & TypeFlags.TypeParameter && (type as TypeParameter).isThisType);
|
||||
}
|
||||
}
|
||||
|
||||
@ -500,6 +500,9 @@ namespace ts {
|
||||
isClass(): this is InterfaceType {
|
||||
return !!(getObjectFlags(this) & ObjectFlags.Class);
|
||||
}
|
||||
isIndexType(): this is IndexType {
|
||||
return !!(this.flags & TypeFlags.Index);
|
||||
}
|
||||
/**
|
||||
* This polyfills `referenceType.typeArguments` for API consumers
|
||||
*/
|
||||
@ -545,6 +548,16 @@ namespace ts {
|
||||
getReturnType(): Type {
|
||||
return this.checker.getReturnTypeOfSignature(this);
|
||||
}
|
||||
getTypeParameterAtPosition(pos: number): Type {
|
||||
const type = this.checker.getParameterType(this, pos);
|
||||
if (type.isIndexType() && isThisTypeParameter(type.type)) {
|
||||
const constraint = type.type.getConstraint();
|
||||
if (constraint) {
|
||||
return this.checker.getIndexType(constraint);
|
||||
}
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
getDocumentationComment(): SymbolDisplayPart[] {
|
||||
return this.documentationComment || (this.documentationComment = getDocumentationComment(singleElementArray(this.declaration), this.checker));
|
||||
|
||||
@ -264,7 +264,7 @@ namespace ts.Completions.StringCompletions {
|
||||
checker.getResolvedSignature(argumentInfo.invocation, candidates, argumentInfo.argumentCount);
|
||||
const types = flatMap(candidates, candidate => {
|
||||
if (!signatureHasRestParameter(candidate) && argumentInfo.argumentCount > candidate.parameters.length) return;
|
||||
const type = checker.getParameterType(candidate, argumentInfo.argumentIndex);
|
||||
const type = candidate.getTypeParameterAtPosition(argumentInfo.argumentIndex);
|
||||
isNewIdentifier = isNewIdentifier || !!(type.flags & TypeFlags.String);
|
||||
return getStringLiteralTypes(type, uniques);
|
||||
});
|
||||
|
||||
@ -72,6 +72,7 @@ namespace ts {
|
||||
isTypeParameter(): this is TypeParameter;
|
||||
isClassOrInterface(): this is InterfaceType;
|
||||
isClass(): this is InterfaceType;
|
||||
isIndexType(): this is IndexType;
|
||||
}
|
||||
|
||||
export interface TypeReference {
|
||||
@ -82,6 +83,7 @@ namespace ts {
|
||||
getDeclaration(): SignatureDeclaration;
|
||||
getTypeParameters(): TypeParameter[] | undefined;
|
||||
getParameters(): Symbol[];
|
||||
getTypeParameterAtPosition(pos: number): Type;
|
||||
getReturnType(): Type;
|
||||
getDocumentationComment(typeChecker: TypeChecker | undefined): SymbolDisplayPart[];
|
||||
getJsDocTags(): JSDocTagInfo[];
|
||||
|
||||
@ -5560,6 +5560,7 @@ declare namespace ts {
|
||||
isTypeParameter(): this is TypeParameter;
|
||||
isClassOrInterface(): this is InterfaceType;
|
||||
isClass(): this is InterfaceType;
|
||||
isIndexType(): this is IndexType;
|
||||
}
|
||||
interface TypeReference {
|
||||
typeArguments?: readonly Type[];
|
||||
@ -5568,6 +5569,7 @@ declare namespace ts {
|
||||
getDeclaration(): SignatureDeclaration;
|
||||
getTypeParameters(): TypeParameter[] | undefined;
|
||||
getParameters(): Symbol[];
|
||||
getTypeParameterAtPosition(pos: number): Type;
|
||||
getReturnType(): Type;
|
||||
getDocumentationComment(typeChecker: TypeChecker | undefined): SymbolDisplayPart[];
|
||||
getJsDocTags(): JSDocTagInfo[];
|
||||
|
||||
@ -5560,6 +5560,7 @@ declare namespace ts {
|
||||
isTypeParameter(): this is TypeParameter;
|
||||
isClassOrInterface(): this is InterfaceType;
|
||||
isClass(): this is InterfaceType;
|
||||
isIndexType(): this is IndexType;
|
||||
}
|
||||
interface TypeReference {
|
||||
typeArguments?: readonly Type[];
|
||||
@ -5568,6 +5569,7 @@ declare namespace ts {
|
||||
getDeclaration(): SignatureDeclaration;
|
||||
getTypeParameters(): TypeParameter[] | undefined;
|
||||
getParameters(): Symbol[];
|
||||
getTypeParameterAtPosition(pos: number): Type;
|
||||
getReturnType(): Type;
|
||||
getDocumentationComment(typeChecker: TypeChecker | undefined): SymbolDisplayPart[];
|
||||
getJsDocTags(): JSDocTagInfo[];
|
||||
|
||||
19
tests/cases/fourslash/completionListAtThisType.ts
Normal file
19
tests/cases/fourslash/completionListAtThisType.ts
Normal file
@ -0,0 +1,19 @@
|
||||
/// <reference path='fourslash.ts'/>
|
||||
|
||||
////class Test {
|
||||
//// foo() {}
|
||||
////
|
||||
//// bar() {
|
||||
//// this.baz(this, "/*1*/");
|
||||
////
|
||||
//// const t = new Test()
|
||||
//// this.baz(t, "/*2*/");
|
||||
//// }
|
||||
////
|
||||
//// baz<T>(a: T, k: keyof T) {}
|
||||
////}
|
||||
|
||||
verify.completions({
|
||||
marker: ["1", "2"],
|
||||
exact: ["foo", "bar", "baz"]
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user