Skip to content

Don't add completions from a discriminated union type when the discriminant doesn't match #24770

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
5 commits merged into from
Jul 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ namespace ts {
getDeclaredTypeOfSymbol,
getPropertiesOfType,
getPropertyOfType: (type, name) => getPropertyOfType(type, escapeLeadingUnderscores(name)),
getTypeOfPropertyOfType: (type, name) => getTypeOfPropertyOfType(type, escapeLeadingUnderscores(name)),
getIndexInfoOfType,
getSignaturesOfType,
getIndexTypeOfType,
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2883,6 +2883,7 @@ namespace ts {
getDeclaredTypeOfSymbol(symbol: Symbol): Type;
getPropertiesOfType(type: Type): Symbol[];
getPropertyOfType(type: Type, propertyName: string): Symbol | undefined;
/* @internal */ getTypeOfPropertyOfType(type: Type, propertyName: string): Type | undefined;
getIndexInfoOfType(type: Type, kind: IndexKind): IndexInfo | undefined;
getSignaturesOfType(type: Type, kind: SignatureKind): Signature[];
getIndexTypeOfType(type: Type, kind: IndexKind): Type | undefined;
Expand Down
40 changes: 28 additions & 12 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ namespace ts.Completions {
// each individual type has. This is because we're going to add all identifiers
// anyways. So we might as well elevate the members that were at least part
// of the individual types to a higher status since we know what they are.
symbols.push(...getPropertiesForCompletion(type, typeChecker, /*isForAccess*/ true));
symbols.push(...getPropertiesForCompletion(type, typeChecker));
}
else {
for (const symbol of type.getApparentProperties()) {
Expand Down Expand Up @@ -1198,7 +1198,7 @@ namespace ts.Completions {
if (preferences.includeCompletionsWithInsertText && scopeNode.kind !== SyntaxKind.SourceFile) {
const thisType = typeChecker.tryGetThisTypeAt(scopeNode);
if (thisType) {
for (const symbol of getPropertiesForCompletion(thisType, typeChecker, /*isForAccess*/ true)) {
for (const symbol of getPropertiesForCompletion(thisType, typeChecker)) {
symbolToOriginInfoMap[getSymbolId(symbol)] = { type: "this-type" };
symbols.push(symbol);
}
Expand Down Expand Up @@ -1515,7 +1515,7 @@ namespace ts.Completions {
const typeForObject = typeChecker.getContextualType(objectLikeContainer);
if (!typeForObject) return GlobalsSearch.Fail;
isNewIdentifierLocation = hasIndexSignature(typeForObject);
typeMembers = getPropertiesForCompletion(typeForObject, typeChecker, /*isForAccess*/ false);
typeMembers = getPropertiesForObjectExpression(typeForObject, objectLikeContainer, typeChecker);
existingMembers = objectLikeContainer.properties;
}
else {
Expand Down Expand Up @@ -2160,19 +2160,35 @@ namespace ts.Completions {
}
}

function getPropertiesForObjectExpression(contextualType: Type, obj: ObjectLiteralExpression, checker: TypeChecker): Symbol[] {
return contextualType.isUnion()
? checker.getAllPossiblePropertiesOfTypes(contextualType.types.filter(memberType => isAllowedUnionTypeForObjectExpression(memberType, obj, checker)))
: contextualType.getApparentProperties();
}
function isAllowedUnionTypeForObjectExpression(contextualType: Type, obj: ObjectLiteralExpression, checker: TypeChecker): boolean {
// If we're providing completions for an object literal, skip primitive, array-like, or callable types since those shouldn't be implemented by object literals.
return !(contextualType.flags & TypeFlags.Primitive || checker.isArrayLikeType(contextualType) || typeHasCallOrConstructSignatures(contextualType, checker)) &&
// In `{ kind: "a", | }`, we only want completions from contextual types where `kind` is "a".
obj.properties.every(property => {
const name = property.name && getTextOfPropertyName(property.name);
const expected = name === undefined ? undefined : checker.getTypeOfPropertyOfType(contextualType, unescapeLeadingUnderscores(name));
if (expected && expected.isLiteral()) {
const actual = checker.getTypeAtLocation(property);
// Apparently two literal types for the same literal are still not equal.
return !actual || actual.isLiteral() && actual.value === expected.value;
}
return true;
});
}

/**
* Gets all properties on a type, but if that type is a union of several types,
* excludes array-like types or callable/constructable types.
*/
function getPropertiesForCompletion(type: Type, checker: TypeChecker, isForAccess: boolean): Symbol[] {
if (!(type.isUnion())) {
return Debug.assertEachDefined(type.getApparentProperties(), "getApparentProperties() should all be defined");
}

// If we're providing completions for an object literal, skip primitive, array-like, or callable types since those shouldn't be implemented by object literals.
const filteredTypes = isForAccess ? type.types : type.types.filter(memberType =>
!(memberType.flags & TypeFlags.Primitive || checker.isArrayLikeType(memberType) || typeHasCallOrConstructSignatures(memberType, checker)));
return Debug.assertEachDefined(checker.getAllPossiblePropertiesOfTypes(filteredTypes), "getAllPossiblePropertiesOfTypes() should all be defined");
function getPropertiesForCompletion(type: Type, checker: TypeChecker): Symbol[] {
return type.isUnion()
? Debug.assertEachDefined(checker.getAllPossiblePropertiesOfTypes(type.types), "getAllPossiblePropertiesOfTypes() should all be defined")
: Debug.assertEachDefined(type.getApparentProperties(), "getApparentProperties() should all be defined");
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/cases/fourslash/completionsDiscriminatedUnion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// <reference path="fourslash.ts" />

////interface A { kind: "a"; a: number; }
////interface B { kind: "b"; b: number; }
////const c: A | B = { kind: "a", /**/ };

verify.completions({ marker: "", exact: ["a"] });