Skip to content

Commit 4a86bc6

Browse files
committed
Add review suggestions
Move object destructuring assignment to checkObjectLiteralAssignment Only check assignability of types in checkVariableLikeDeclaration for object/array destructuring when there are properties present in the pattern.
1 parent b9543bf commit 4a86bc6

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

src/compiler/checker.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18769,6 +18769,9 @@ namespace ts {
1876918769

1877018770
function checkObjectLiteralAssignment(node: ObjectLiteralExpression, sourceType: Type): Type {
1877118771
const properties = node.properties;
18772+
if (strictNullChecks && properties.length === 0) {
18773+
return checkNonNullType(sourceType, node);
18774+
}
1877218775
for (const p of properties) {
1877318776
checkObjectLiteralDestructuringPropertyAssignment(sourceType, p, properties);
1877418777
}
@@ -18908,9 +18911,6 @@ namespace ts {
1890818911
target = (<BinaryExpression>target).left;
1890918912
}
1891018913
if (target.kind === SyntaxKind.ObjectLiteralExpression) {
18911-
if (strictNullChecks && (<ObjectLiteralExpression>target).properties.length === 0) {
18912-
return checkNonNullType(sourceType, target);
18913-
}
1891418914
return checkObjectLiteralAssignment(<ObjectLiteralExpression>target, sourceType);
1891518915
}
1891618916
if (target.kind === SyntaxKind.ArrayLiteralExpression) {
@@ -21836,12 +21836,14 @@ namespace ts {
2183621836
if (isBindingPattern(node.name)) {
2183721837
// Don't validate for-in initializer as it is already an error
2183821838
if (node.initializer && node.parent.parent.kind !== SyntaxKind.ForInStatement) {
21839-
let initializerType = checkExpressionCached(node.initializer);
21839+
const initializerType = checkExpressionCached(node.initializer);
2184021840
if (strictNullChecks && node.name.elements.length === 0) {
21841-
initializerType = checkNonNullType(initializerType, node);
21841+
checkNonNullType(initializerType, node);
21842+
}
21843+
else {
21844+
checkTypeAssignableTo(initializerType, getWidenedTypeForVariableLikeDeclaration(node), node, /*headMessage*/ undefined);
21845+
checkParameterInitializer(node);
2184221846
}
21843-
checkTypeAssignableTo(initializerType, getWidenedTypeForVariableLikeDeclaration(node), node, /*headMessage*/ undefined);
21844-
checkParameterInitializer(node);
2184521847
}
2184621848
return;
2184721849
}

0 commit comments

Comments
 (0)