Skip to content

Commit e12b708

Browse files
author
Andy Hanson
committed
For completions of union type, get all possible properties
1 parent 7a0ee3c commit e12b708

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

src/compiler/checker.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ namespace ts {
204204
// since we are only interested in declarations of the module itself
205205
return tryFindAmbientModule(moduleName, /*withAugmentations*/ false);
206206
},
207-
getApparentType
207+
getApparentType,
208+
getAllPossiblePropertiesOfType,
208209
};
209210

210211
const tupleTypes: GenericType[] = [];
@@ -5648,6 +5649,22 @@ namespace ts {
56485649
getPropertiesOfObjectType(type);
56495650
}
56505651

5652+
function getAllPossiblePropertiesOfType(type: Type): Symbol[] {
5653+
if (type.flags & TypeFlags.Union) {
5654+
const props = createMap<Symbol>();
5655+
for (const memberType of (type as UnionType).types) {
5656+
for (const { name } of getPropertiesOfType(memberType)) {
5657+
if (!props.has(name)) {
5658+
props.set(name, createUnionOrIntersectionProperty(type as UnionType, name));
5659+
}
5660+
}
5661+
}
5662+
return arrayFrom(props.values());
5663+
} else {
5664+
return getPropertiesOfType(type);
5665+
}
5666+
}
5667+
56515668
function getConstraintOfType(type: TypeVariable | UnionOrIntersectionType): Type {
56525669
return type.flags & TypeFlags.TypeParameter ? getConstraintOfTypeParameter(<TypeParameter>type) :
56535670
type.flags & TypeFlags.IndexedAccess ? getConstraintOfIndexedAccess(<IndexedAccessType>type) :

src/compiler/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2564,6 +2564,11 @@ namespace ts {
25642564
/* @internal */ getIdentifierCount(): number;
25652565
/* @internal */ getSymbolCount(): number;
25662566
/* @internal */ getTypeCount(): number;
2567+
2568+
/** For a union, will include a property if it's defined in *any* of the member types.
2569+
* So for `{ a } | { b }`, this will include both `a` and `b`.
2570+
*/
2571+
/* @internal */ getAllPossiblePropertiesOfType(type: Type): Symbol[];
25672572
}
25682573

25692574
export enum NodeBuilderFlags {

src/services/completions.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -813,19 +813,16 @@ namespace ts.Completions {
813813
// We're looking up possible property names from contextual/inferred/declared type.
814814
isMemberCompletion = true;
815815

816-
let typeForObject: Type;
816+
let typeMembers: Symbol[];
817817
let existingMembers: Declaration[];
818818

819819
if (objectLikeContainer.kind === SyntaxKind.ObjectLiteralExpression) {
820820
// We are completing on contextual types, but may also include properties
821821
// other than those within the declared type.
822822
isNewIdentifierLocation = true;
823-
824-
// If the object literal is being assigned to something of type 'null | { hello: string }',
825-
// it clearly isn't trying to satisfy the 'null' type. So we grab the non-nullable type if possible.
826-
typeForObject = typeChecker.getContextualType(<ObjectLiteralExpression>objectLikeContainer);
827-
typeForObject = typeForObject && typeForObject.getNonNullableType();
828-
823+
const typeForObject = typeChecker.getContextualType(<ObjectLiteralExpression>objectLikeContainer);
824+
if (!typeForObject) return false;
825+
typeMembers = typeChecker.getAllPossiblePropertiesOfType(typeForObject);
829826
existingMembers = (<ObjectLiteralExpression>objectLikeContainer).properties;
830827
}
831828
else if (objectLikeContainer.kind === SyntaxKind.ObjectBindingPattern) {
@@ -849,7 +846,10 @@ namespace ts.Completions {
849846
}
850847
}
851848
if (canGetType) {
852-
typeForObject = typeChecker.getTypeAtLocation(objectLikeContainer);
849+
const typeForObject = typeChecker.getTypeAtLocation(objectLikeContainer);
850+
if (!typeForObject) return false;
851+
// In a binding pattern, get only known properties. Everywhere else we will get all possible properties.
852+
typeMembers = typeChecker.getPropertiesOfType(typeForObject);
853853
existingMembers = (<ObjectBindingPattern>objectLikeContainer).elements;
854854
}
855855
}
@@ -861,11 +861,6 @@ namespace ts.Completions {
861861
Debug.fail("Expected object literal or binding pattern, got " + objectLikeContainer.kind);
862862
}
863863

864-
if (!typeForObject) {
865-
return false;
866-
}
867-
868-
const typeMembers = typeChecker.getPropertiesOfType(typeForObject);
869864
if (typeMembers && typeMembers.length > 0) {
870865
// Add filtered items to the completion list
871866
symbols = filterObjectMembersList(typeMembers, existingMembers);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @strictNullChecks: true
4+
5+
// Non-objects should be skipped, so `| number | null` should have no effect on completions.
6+
////const x: { a: number, b: number } | { a: string, c: string } | { b: boolean } | number | null = { /*x*/ };
7+
8+
////interface I { a: number; }
9+
////function f(...args: Array<I | I[]>) {}
10+
////f({ /*f*/ });
11+
12+
goTo.marker("x");
13+
verify.completionListContains("a", "(property) a: string | number");
14+
verify.completionListContains("b", "(property) b: number | boolean");
15+
verify.completionListContains("c", "(property) c: string");
16+
17+
goTo.marker("f");
18+
verify.completionListContains("a", "(property) a: number");

0 commit comments

Comments
 (0)