Skip to content

Fix completions for nullable objects #10403

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
merged 2 commits into from
Aug 18, 2016
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
6 changes: 5 additions & 1 deletion src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3789,7 +3789,11 @@ namespace ts {
// other than those within the declared type.
isNewIdentifierLocation = true;

// If the object literal is being assigned to something of type 'null | { hello: string }',
// it clearly isn't trying to satisfy the 'null' type. So we grab the non-nullable type if possible.
typeForObject = typeChecker.getContextualType(<ObjectLiteralExpression>objectLikeContainer);
typeForObject = typeForObject && typeForObject.getNonNullableType();

existingMembers = (<ObjectLiteralExpression>objectLikeContainer).properties;
}
else if (objectLikeContainer.kind === SyntaxKind.ObjectBindingPattern) {
Expand All @@ -3801,7 +3805,7 @@ namespace ts {
// We don't want to complete using the type acquired by the shape
// of the binding pattern; we are only interested in types acquired
// through type declaration or inference.
// Also proceed if rootDeclaration is parameter and if its containing function expression\arrow function is contextually typed -
// Also proceed if rootDeclaration is a parameter and if its containing function expression/arrow function is contextually typed -
// type of parameter will flow in from the contextual type of the function
let canGetType = !!(rootDeclaration.initializer || rootDeclaration.type);
if (!canGetType && rootDeclaration.kind === SyntaxKind.Parameter) {
Expand Down
28 changes: 28 additions & 0 deletions tests/cases/fourslash/completionListInObjectLiteral4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// <reference path='fourslash.ts'/>

// @strictNullChecks: true
////interface Thing {
//// hello: number;
//// world: string;
////}
////
////declare function funcA(x : Thing): void;
////declare function funcB(x?: Thing): void;
////declare function funcC(x : Thing | null): void;
////declare function funcD(x : Thing | undefined): void;
////declare function funcE(x : Thing | null | undefined): void;
////declare function funcF(x?: Thing | null | undefined): void;
////
////funcA({ /*A*/ });
////funcB({ /*B*/ });
////funcC({ /*C*/ });
////funcD({ /*D*/ });
////funcE({ /*E*/ });
////funcF({ /*F*/ });


for (const marker of test.markers()) {
goTo.position(marker.position);
verify.completionListContains("hello");
verify.completionListContains("world");
}