Skip to content

Commit c1e70c9

Browse files
Use the non-nullable type of the contextual type for object completions.
1 parent 111b7c5 commit c1e70c9

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/services/services.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3789,7 +3789,11 @@ namespace ts {
37893789
// other than those within the declared type.
37903790
isNewIdentifierLocation = true;
37913791

3792+
// If the object literal is being assigned to something of type 'null | { hello: string }',
3793+
// it clearly isn't trying to satisfy the 'null' type. So we grab the non-nullable type if possible.
37923794
typeForObject = typeChecker.getContextualType(<ObjectLiteralExpression>objectLikeContainer);
3795+
typeForObject = typeForObject && typeForObject.getNonNullableType();
3796+
37933797
existingMembers = (<ObjectLiteralExpression>objectLikeContainer).properties;
37943798
}
37953799
else if (objectLikeContainer.kind === SyntaxKind.ObjectBindingPattern) {
@@ -3801,7 +3805,7 @@ namespace ts {
38013805
// We don't want to complete using the type acquired by the shape
38023806
// of the binding pattern; we are only interested in types acquired
38033807
// through type declaration or inference.
3804-
// Also proceed if rootDeclaration is parameter and if its containing function expression\arrow function is contextually typed -
3808+
// Also proceed if rootDeclaration is a parameter and if its containing function expression/arrow function is contextually typed -
38053809
// type of parameter will flow in from the contextual type of the function
38063810
let canGetType = !!(rootDeclaration.initializer || rootDeclaration.type);
38073811
if (!canGetType && rootDeclaration.kind === SyntaxKind.Parameter) {

0 commit comments

Comments
 (0)