Skip to content

Commit 46ae059

Browse files
authored
Merge pull request microsoft#30695 from andrewbranch/bug/30638
Add type error when destructuring zero elements from void
2 parents ca98a50 + abd448f commit 46ae059

10 files changed

+66
-1
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19518,6 +19518,14 @@ namespace ts {
1951819518
return type;
1951919519
}
1952019520

19521+
function checkNonNullNonVoidType(type: Type, node: Node): Type {
19522+
const nonNullType = checkNonNullType(type, node);
19523+
if (nonNullType !== errorType && nonNullType.flags & TypeFlags.Void) {
19524+
error(node, Diagnostics.Object_is_possibly_undefined);
19525+
}
19526+
return nonNullType;
19527+
}
19528+
1952119529
function checkPropertyAccessExpression(node: PropertyAccessExpression) {
1952219530
return checkPropertyAccessExpressionOrQualifiedName(node, node.expression, node.name);
1952319531
}
@@ -26258,7 +26266,7 @@ namespace ts {
2625826266
if (node.initializer && node.parent.parent.kind !== SyntaxKind.ForInStatement) {
2625926267
const initializerType = checkExpressionCached(node.initializer);
2626026268
if (strictNullChecks && node.name.elements.length === 0) {
26261-
checkNonNullType(initializerType, node);
26269+
checkNonNullNonVoidType(initializerType, node);
2626226270
}
2626326271
else {
2626426272
checkTypeAssignableToAndOptionallyElaborate(initializerType, getWidenedTypeForVariableLikeDeclaration(node), node, node.initializer);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [destructuringVoid.ts]
2+
declare const v: void;
3+
const {} = v;
4+
5+
6+
//// [destructuringVoid.js]
7+
var _a = v;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/conformance/es6/destructuring/destructuringVoid.ts ===
2+
declare const v: void;
3+
>v : Symbol(v, Decl(destructuringVoid.ts, 0, 13))
4+
5+
const {} = v;
6+
>v : Symbol(v, Decl(destructuringVoid.ts, 0, 13))
7+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/conformance/es6/destructuring/destructuringVoid.ts ===
2+
declare const v: void;
3+
>v : void
4+
5+
const {} = v;
6+
>v : void
7+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/conformance/es6/destructuring/destructuringVoidStrictNullChecks.ts(2,7): error TS2532: Object is possibly 'undefined'.
2+
3+
4+
==== tests/cases/conformance/es6/destructuring/destructuringVoidStrictNullChecks.ts (1 errors) ====
5+
declare const v: void;
6+
const {} = v;
7+
~~
8+
!!! error TS2532: Object is possibly 'undefined'.
9+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [destructuringVoidStrictNullChecks.ts]
2+
declare const v: void;
3+
const {} = v;
4+
5+
6+
//// [destructuringVoidStrictNullChecks.js]
7+
var _a = v;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/conformance/es6/destructuring/destructuringVoidStrictNullChecks.ts ===
2+
declare const v: void;
3+
>v : Symbol(v, Decl(destructuringVoidStrictNullChecks.ts, 0, 13))
4+
5+
const {} = v;
6+
>v : Symbol(v, Decl(destructuringVoidStrictNullChecks.ts, 0, 13))
7+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/conformance/es6/destructuring/destructuringVoidStrictNullChecks.ts ===
2+
declare const v: void;
3+
>v : void
4+
5+
const {} = v;
6+
>v : void
7+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @strictNullChecks: false
2+
declare const v: void;
3+
const {} = v;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @strictNullChecks: true
2+
declare const v: void;
3+
const {} = v;

0 commit comments

Comments
 (0)