Provide string completions for in keyword checks (#60272)

This commit is contained in:
Mateusz Burzyński
2025-02-20 23:01:52 +01:00
committed by GitHub
parent 0d016920e6
commit b95187d1ce
2 changed files with 54 additions and 1 deletions

View File

@@ -12,6 +12,7 @@ import {
addToSeen,
altDirectorySeparator,
arrayFrom,
BinaryExpression,
CallLikeExpression,
CancellationToken,
CaseClause,
@@ -491,7 +492,17 @@ function getStringLiteralCompletionEntries(sourceFile: SourceFile, node: StringL
const existing = new Set(namedImportsOrExports.elements.map(n => moduleExportNameTextEscaped(n.propertyName || n.name)));
const uniques = exports.filter(e => e.escapedName !== InternalSymbolName.Default && !existing.has(e.escapedName));
return { kind: StringLiteralCompletionKind.Properties, symbols: uniques, hasIndexSignature: false };
case SyntaxKind.BinaryExpression:
if ((parent as BinaryExpression).operatorToken.kind === SyntaxKind.InKeyword) {
const type = typeChecker.getTypeAtLocation((parent as BinaryExpression).right);
const properties = type.isUnion() ? typeChecker.getAllPossiblePropertiesOfTypes(type.types) : type.getApparentProperties();
return {
kind: StringLiteralCompletionKind.Properties,
symbols: properties.filter(prop => !prop.valueDeclaration || !isPrivateIdentifierClassElementDeclaration(prop.valueDeclaration)),
hasIndexSignature: false,
};
}
return fromContextualType(ContextFlags.None);
default:
return fromContextualType() || fromContextualType(ContextFlags.None);
}