Skip to content

Commit a621c09

Browse files
authored
Merge pull request #8241 from Microsoft/noImplicitAnyDestructuring
Do not report errors of implicit any during contexual type checking of binding pattern element
2 parents b8963ba + 02f908a commit a621c09

8 files changed

+290
-24
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3083,7 +3083,7 @@ namespace ts {
30833083

30843084
// If the declaration specifies a binding pattern, use the type implied by the binding pattern
30853085
if (isBindingPattern(declaration.name)) {
3086-
return getTypeFromBindingPattern(<BindingPattern>declaration.name, /*includePatternInType*/ false);
3086+
return getTypeFromBindingPattern(<BindingPattern>declaration.name, /*includePatternInType*/ false, /*reportErrors*/ true);
30873087
}
30883088

30893089
// No type specified and nothing can be inferred
@@ -3093,23 +3093,21 @@ namespace ts {
30933093
// Return the type implied by a binding pattern element. This is the type of the initializer of the element if
30943094
// one is present. Otherwise, if the element is itself a binding pattern, it is the type implied by the binding
30953095
// pattern. Otherwise, it is the type any.
3096-
function getTypeFromBindingElement(element: BindingElement, includePatternInType?: boolean): Type {
3096+
function getTypeFromBindingElement(element: BindingElement, includePatternInType?: boolean, reportErrors?: boolean): Type {
30973097
if (element.initializer) {
3098-
const type = checkExpressionCached(element.initializer);
3099-
reportErrorsFromWidening(element, type);
3100-
return getWidenedType(type);
3098+
return checkExpressionCached(element.initializer);
31013099
}
31023100
if (isBindingPattern(element.name)) {
3103-
return getTypeFromBindingPattern(<BindingPattern>element.name, includePatternInType);
3101+
return getTypeFromBindingPattern(<BindingPattern>element.name, includePatternInType, reportErrors);
31043102
}
3105-
if (compilerOptions.noImplicitAny && !declarationBelongsToPrivateAmbientMember(element)) {
3103+
if (reportErrors && compilerOptions.noImplicitAny && !declarationBelongsToPrivateAmbientMember(element)) {
31063104
reportImplicitAnyError(element, anyType);
31073105
}
31083106
return anyType;
31093107
}
31103108

31113109
// Return the type implied by an object binding pattern
3112-
function getTypeFromObjectBindingPattern(pattern: BindingPattern, includePatternInType: boolean): Type {
3110+
function getTypeFromObjectBindingPattern(pattern: BindingPattern, includePatternInType: boolean, reportErrors: boolean): Type {
31133111
const members = createMap<Symbol>();
31143112
let hasComputedProperties = false;
31153113
forEach(pattern.elements, e => {
@@ -3123,7 +3121,7 @@ namespace ts {
31233121
const text = getTextOfPropertyName(name);
31243122
const flags = SymbolFlags.Property | SymbolFlags.Transient | (e.initializer ? SymbolFlags.Optional : 0);
31253123
const symbol = <TransientSymbol>createSymbol(flags, text);
3126-
symbol.type = getTypeFromBindingElement(e, includePatternInType);
3124+
symbol.type = getTypeFromBindingElement(e, includePatternInType, reportErrors);
31273125
symbol.bindingElement = e;
31283126
members[symbol.name] = symbol;
31293127
});
@@ -3138,13 +3136,13 @@ namespace ts {
31383136
}
31393137

31403138
// Return the type implied by an array binding pattern
3141-
function getTypeFromArrayBindingPattern(pattern: BindingPattern, includePatternInType: boolean): Type {
3139+
function getTypeFromArrayBindingPattern(pattern: BindingPattern, includePatternInType: boolean, reportErrors: boolean): Type {
31423140
const elements = pattern.elements;
31433141
if (elements.length === 0 || elements[elements.length - 1].dotDotDotToken) {
31443142
return languageVersion >= ScriptTarget.ES6 ? createIterableType(anyType) : anyArrayType;
31453143
}
31463144
// If the pattern has at least one element, and no rest element, then it should imply a tuple type.
3147-
const elementTypes = map(elements, e => e.kind === SyntaxKind.OmittedExpression ? anyType : getTypeFromBindingElement(e, includePatternInType));
3145+
const elementTypes = map(elements, e => e.kind === SyntaxKind.OmittedExpression ? anyType : getTypeFromBindingElement(e, includePatternInType, reportErrors));
31483146
if (includePatternInType) {
31493147
const result = createNewTupleType(elementTypes);
31503148
result.pattern = pattern;
@@ -3160,10 +3158,10 @@ namespace ts {
31603158
// used as the contextual type of an initializer associated with the binding pattern. Also, for a destructuring
31613159
// parameter with no type annotation or initializer, the type implied by the binding pattern becomes the type of
31623160
// the parameter.
3163-
function getTypeFromBindingPattern(pattern: BindingPattern, includePatternInType?: boolean): Type {
3161+
function getTypeFromBindingPattern(pattern: BindingPattern, includePatternInType?: boolean, reportErrors?: boolean): Type {
31643162
return pattern.kind === SyntaxKind.ObjectBindingPattern
3165-
? getTypeFromObjectBindingPattern(pattern, includePatternInType)
3166-
: getTypeFromArrayBindingPattern(pattern, includePatternInType);
3163+
? getTypeFromObjectBindingPattern(pattern, includePatternInType, reportErrors)
3164+
: getTypeFromArrayBindingPattern(pattern, includePatternInType, reportErrors);
31673165
}
31683166

31693167
// Return the type associated with a variable, parameter, or property declaration. In the simple case this is the type
@@ -9414,7 +9412,7 @@ namespace ts {
94149412
}
94159413
}
94169414
if (isBindingPattern(declaration.name)) {
9417-
return getTypeFromBindingPattern(<BindingPattern>declaration.name, /*includePatternInType*/ true);
9415+
return getTypeFromBindingPattern(<BindingPattern>declaration.name, /*includePatternInType*/ true, /*reportErrors*/ false);
94189416
}
94199417
if (isBindingPattern(declaration.parent)) {
94209418
const parentDeclaration = declaration.parent.parent;

tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration.errors.txt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(5,18): error TS
1515
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(7,5): error TS1182: A destructuring declaration must have an initializer.
1616
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(7,13): error TS7008: Member 'b3' implicitly has an 'any' type.
1717
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(7,25): error TS7008: Member 'b3' implicitly has an 'any' type.
18-
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,6): error TS7031: Binding element 'a1' implicitly has an 'any' type.
19-
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,26): error TS7031: Binding element 'b1' implicitly has an 'any' type.
18+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,6): error TS7031: Binding element 'a4' implicitly has an 'any' type.
19+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,26): error TS7031: Binding element 'b4' implicitly has an 'any' type.
20+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,46): error TS7005: Variable 'c4' implicitly has an 'any' type.
21+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,62): error TS7005: Variable 'd4' implicitly has an 'any' type.
22+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(11,6): error TS7031: Binding element 'a5' implicitly has an 'any' type.
2023

2124

22-
==== tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts (19 errors) ====
25+
==== tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts (22 errors) ====
2326
var [a], {b}, c, d; // error
2427
~~~
2528
!!! error TS1182: A destructuring declaration must have an initializer.
@@ -62,8 +65,16 @@ tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(9,26): error TS
6265
~~
6366
!!! error TS7008: Member 'b3' implicitly has an 'any' type.
6467

65-
var [a1] = [undefined], {b1} = { b1: null }, c1 = undefined, d1 = null; // error
68+
var [a4] = [undefined], {b4} = { b4: null }, c4 = undefined, d4 = null; // error
6669
~~
67-
!!! error TS7031: Binding element 'a1' implicitly has an 'any' type.
70+
!!! error TS7031: Binding element 'a4' implicitly has an 'any' type.
6871
~~
69-
!!! error TS7031: Binding element 'b1' implicitly has an 'any' type.
72+
!!! error TS7031: Binding element 'b4' implicitly has an 'any' type.
73+
~~
74+
!!! error TS7005: Variable 'c4' implicitly has an 'any' type.
75+
~~
76+
!!! error TS7005: Variable 'd4' implicitly has an 'any' type.
77+
78+
var [a5 = undefined] = []; // error
79+
~~
80+
!!! error TS7031: Binding element 'a5' implicitly has an 'any' type.

tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ var [a2]: [any], {b2}: { b2: any }, c2: any, d2: any;
77

88
var {b3}: { b3 }, c3: { b3 }; // error in type instead
99

10-
var [a1] = [undefined], {b1} = { b1: null }, c1 = undefined, d1 = null; // error
10+
var [a4] = [undefined], {b4} = { b4: null }, c4 = undefined, d4 = null; // error
11+
12+
var [a5 = undefined] = []; // error
1113

1214
//// [noImplicitAnyDestructuringVarDeclaration.js]
1315
var a = (void 0)[0], b = (void 0).b, c, d; // error
1416
var _a = (void 0)[0], a1 = _a === void 0 ? undefined : _a, _b = (void 0).b1, b1 = _b === void 0 ? null : _b, c1 = undefined, d1 = null; // error
1517
var a2 = (void 0)[0], b2 = (void 0).b2, c2, d2;
1618
var b3 = (void 0).b3, c3; // error in type instead
17-
var a1 = [undefined][0], b1 = { b1: null }.b1, c1 = undefined, d1 = null; // error
19+
var a4 = [undefined][0], b4 = { b4: null }.b4, c4 = undefined, d4 = null; // error
20+
var _c = [][0], a5 = _c === void 0 ? undefined : _c; // error
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [noImplicitAnyDestructuringVarDeclaration2.ts]
2+
let [a, b, c] = [1, 2, 3]; // no error
3+
let [a1 = 10, b1 = 10, c1 = 10] = [1, 2, 3]; // no error
4+
let [a2 = undefined, b2 = undefined, c2 = undefined] = [1, 2, 3]; // no error
5+
let [a3 = <any>undefined, b3 = <any>null, c3 = <any>undefined] = [1, 2, 3]; // no error
6+
let [a4] = [<any>undefined], [b4] = [<any>null], c4 = <any>undefined, d4 = <any>null; // no error
7+
8+
let {x, y, z} = { x: 1, y: 2, z: 3 }; // no error
9+
let {x1 = 10, y1 = 10, z1 = 10} = { x1: 1, y1: 2, z1: 3 }; // no error
10+
let {x2 = undefined, y2 = undefined, z2 = undefined} = { x2: 1, y2: 2, z2: 3 }; // no error
11+
let {x3 = <any>undefined, y3 = <any>null, z3 = <any>undefined} = { x3: 1, y3: 2, z3: 3 }; // no error
12+
let {x4} = { x4: <any>undefined }, {y4} = { y4: <any>null }; // no error
13+
14+
15+
//// [noImplicitAnyDestructuringVarDeclaration2.js]
16+
var _a = [1, 2, 3], a = _a[0], b = _a[1], c = _a[2]; // no error
17+
var _b = [1, 2, 3], _c = _b[0], a1 = _c === void 0 ? 10 : _c, _d = _b[1], b1 = _d === void 0 ? 10 : _d, _e = _b[2], c1 = _e === void 0 ? 10 : _e; // no error
18+
var _f = [1, 2, 3], _g = _f[0], a2 = _g === void 0 ? undefined : _g, _h = _f[1], b2 = _h === void 0 ? undefined : _h, _j = _f[2], c2 = _j === void 0 ? undefined : _j; // no error
19+
var _k = [1, 2, 3], _l = _k[0], a3 = _l === void 0 ? undefined : _l, _m = _k[1], b3 = _m === void 0 ? null : _m, _o = _k[2], c3 = _o === void 0 ? undefined : _o; // no error
20+
var a4 = [undefined][0], b4 = [null][0], c4 = undefined, d4 = null; // no error
21+
var _p = { x: 1, y: 2, z: 3 }, x = _p.x, y = _p.y, z = _p.z; // no error
22+
var _q = { x1: 1, y1: 2, z1: 3 }, _r = _q.x1, x1 = _r === void 0 ? 10 : _r, _s = _q.y1, y1 = _s === void 0 ? 10 : _s, _t = _q.z1, z1 = _t === void 0 ? 10 : _t; // no error
23+
var _u = { x2: 1, y2: 2, z2: 3 }, _v = _u.x2, x2 = _v === void 0 ? undefined : _v, _w = _u.y2, y2 = _w === void 0 ? undefined : _w, _x = _u.z2, z2 = _x === void 0 ? undefined : _x; // no error
24+
var _y = { x3: 1, y3: 2, z3: 3 }, _z = _y.x3, x3 = _z === void 0 ? undefined : _z, _0 = _y.y3, y3 = _0 === void 0 ? null : _0, _1 = _y.z3, z3 = _1 === void 0 ? undefined : _1; // no error
25+
var x4 = { x4: undefined }.x4, y4 = { y4: null }.y4; // no error
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
=== tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts ===
2+
let [a, b, c] = [1, 2, 3]; // no error
3+
>a : Symbol(a, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 0, 5))
4+
>b : Symbol(b, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 0, 7))
5+
>c : Symbol(c, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 0, 10))
6+
7+
let [a1 = 10, b1 = 10, c1 = 10] = [1, 2, 3]; // no error
8+
>a1 : Symbol(a1, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 1, 5))
9+
>b1 : Symbol(b1, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 1, 13))
10+
>c1 : Symbol(c1, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 1, 22))
11+
12+
let [a2 = undefined, b2 = undefined, c2 = undefined] = [1, 2, 3]; // no error
13+
>a2 : Symbol(a2, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 2, 5))
14+
>undefined : Symbol(undefined)
15+
>b2 : Symbol(b2, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 2, 20))
16+
>undefined : Symbol(undefined)
17+
>c2 : Symbol(c2, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 2, 36))
18+
>undefined : Symbol(undefined)
19+
20+
let [a3 = <any>undefined, b3 = <any>null, c3 = <any>undefined] = [1, 2, 3]; // no error
21+
>a3 : Symbol(a3, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 3, 5))
22+
>undefined : Symbol(undefined)
23+
>b3 : Symbol(b3, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 3, 25))
24+
>c3 : Symbol(c3, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 3, 41))
25+
>undefined : Symbol(undefined)
26+
27+
let [a4] = [<any>undefined], [b4] = [<any>null], c4 = <any>undefined, d4 = <any>null; // no error
28+
>a4 : Symbol(a4, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 4, 5))
29+
>undefined : Symbol(undefined)
30+
>b4 : Symbol(b4, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 4, 30))
31+
>c4 : Symbol(c4, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 4, 48))
32+
>undefined : Symbol(undefined)
33+
>d4 : Symbol(d4, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 4, 69))
34+
35+
let {x, y, z} = { x: 1, y: 2, z: 3 }; // no error
36+
>x : Symbol(x, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 6, 5))
37+
>y : Symbol(y, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 6, 7))
38+
>z : Symbol(z, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 6, 10))
39+
>x : Symbol(x, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 6, 17))
40+
>y : Symbol(y, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 6, 23))
41+
>z : Symbol(z, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 6, 29))
42+
43+
let {x1 = 10, y1 = 10, z1 = 10} = { x1: 1, y1: 2, z1: 3 }; // no error
44+
>x1 : Symbol(x1, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 7, 5))
45+
>y1 : Symbol(y1, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 7, 13))
46+
>z1 : Symbol(z1, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 7, 22))
47+
>x1 : Symbol(x1, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 7, 35))
48+
>y1 : Symbol(y1, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 7, 42))
49+
>z1 : Symbol(z1, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 7, 49))
50+
51+
let {x2 = undefined, y2 = undefined, z2 = undefined} = { x2: 1, y2: 2, z2: 3 }; // no error
52+
>x2 : Symbol(x2, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 8, 5))
53+
>undefined : Symbol(undefined)
54+
>y2 : Symbol(y2, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 8, 20))
55+
>undefined : Symbol(undefined)
56+
>z2 : Symbol(z2, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 8, 36))
57+
>undefined : Symbol(undefined)
58+
>x2 : Symbol(x2, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 8, 56))
59+
>y2 : Symbol(y2, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 8, 63))
60+
>z2 : Symbol(z2, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 8, 70))
61+
62+
let {x3 = <any>undefined, y3 = <any>null, z3 = <any>undefined} = { x3: 1, y3: 2, z3: 3 }; // no error
63+
>x3 : Symbol(x3, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 9, 5))
64+
>undefined : Symbol(undefined)
65+
>y3 : Symbol(y3, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 9, 25))
66+
>z3 : Symbol(z3, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 9, 41))
67+
>undefined : Symbol(undefined)
68+
>x3 : Symbol(x3, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 9, 66))
69+
>y3 : Symbol(y3, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 9, 73))
70+
>z3 : Symbol(z3, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 9, 80))
71+
72+
let {x4} = { x4: <any>undefined }, {y4} = { y4: <any>null }; // no error
73+
>x4 : Symbol(x4, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 10, 5))
74+
>x4 : Symbol(x4, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 10, 12))
75+
>undefined : Symbol(undefined)
76+
>y4 : Symbol(y4, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 10, 36))
77+
>y4 : Symbol(y4, Decl(noImplicitAnyDestructuringVarDeclaration2.ts, 10, 43))
78+
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
=== tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration2.ts ===
2+
let [a, b, c] = [1, 2, 3]; // no error
3+
>a : number
4+
>b : number
5+
>c : number
6+
>[1, 2, 3] : [number, number, number]
7+
>1 : number
8+
>2 : number
9+
>3 : number
10+
11+
let [a1 = 10, b1 = 10, c1 = 10] = [1, 2, 3]; // no error
12+
>a1 : number
13+
>10 : number
14+
>b1 : number
15+
>10 : number
16+
>c1 : number
17+
>10 : number
18+
>[1, 2, 3] : [number, number, number]
19+
>1 : number
20+
>2 : number
21+
>3 : number
22+
23+
let [a2 = undefined, b2 = undefined, c2 = undefined] = [1, 2, 3]; // no error
24+
>a2 : number
25+
>undefined : undefined
26+
>b2 : number
27+
>undefined : undefined
28+
>c2 : number
29+
>undefined : undefined
30+
>[1, 2, 3] : [number, number, number]
31+
>1 : number
32+
>2 : number
33+
>3 : number
34+
35+
let [a3 = <any>undefined, b3 = <any>null, c3 = <any>undefined] = [1, 2, 3]; // no error
36+
>a3 : number
37+
><any>undefined : any
38+
>undefined : undefined
39+
>b3 : number
40+
><any>null : any
41+
>null : null
42+
>c3 : number
43+
><any>undefined : any
44+
>undefined : undefined
45+
>[1, 2, 3] : [number, number, number]
46+
>1 : number
47+
>2 : number
48+
>3 : number
49+
50+
let [a4] = [<any>undefined], [b4] = [<any>null], c4 = <any>undefined, d4 = <any>null; // no error
51+
>a4 : any
52+
>[<any>undefined] : [any]
53+
><any>undefined : any
54+
>undefined : undefined
55+
>b4 : any
56+
>[<any>null] : [any]
57+
><any>null : any
58+
>null : null
59+
>c4 : any
60+
><any>undefined : any
61+
>undefined : undefined
62+
>d4 : any
63+
><any>null : any
64+
>null : null
65+
66+
let {x, y, z} = { x: 1, y: 2, z: 3 }; // no error
67+
>x : number
68+
>y : number
69+
>z : number
70+
>{ x: 1, y: 2, z: 3 } : { x: number; y: number; z: number; }
71+
>x : number
72+
>1 : number
73+
>y : number
74+
>2 : number
75+
>z : number
76+
>3 : number
77+
78+
let {x1 = 10, y1 = 10, z1 = 10} = { x1: 1, y1: 2, z1: 3 }; // no error
79+
>x1 : number
80+
>10 : number
81+
>y1 : number
82+
>10 : number
83+
>z1 : number
84+
>10 : number
85+
>{ x1: 1, y1: 2, z1: 3 } : { x1?: number; y1?: number; z1?: number; }
86+
>x1 : number
87+
>1 : number
88+
>y1 : number
89+
>2 : number
90+
>z1 : number
91+
>3 : number
92+
93+
let {x2 = undefined, y2 = undefined, z2 = undefined} = { x2: 1, y2: 2, z2: 3 }; // no error
94+
>x2 : number
95+
>undefined : undefined
96+
>y2 : number
97+
>undefined : undefined
98+
>z2 : number
99+
>undefined : undefined
100+
>{ x2: 1, y2: 2, z2: 3 } : { x2?: number; y2?: number; z2?: number; }
101+
>x2 : number
102+
>1 : number
103+
>y2 : number
104+
>2 : number
105+
>z2 : number
106+
>3 : number
107+
108+
let {x3 = <any>undefined, y3 = <any>null, z3 = <any>undefined} = { x3: 1, y3: 2, z3: 3 }; // no error
109+
>x3 : number
110+
><any>undefined : any
111+
>undefined : undefined
112+
>y3 : number
113+
><any>null : any
114+
>null : null
115+
>z3 : number
116+
><any>undefined : any
117+
>undefined : undefined
118+
>{ x3: 1, y3: 2, z3: 3 } : { x3?: number; y3?: number; z3?: number; }
119+
>x3 : number
120+
>1 : number
121+
>y3 : number
122+
>2 : number
123+
>z3 : number
124+
>3 : number
125+
126+
let {x4} = { x4: <any>undefined }, {y4} = { y4: <any>null }; // no error
127+
>x4 : any
128+
>{ x4: <any>undefined } : { x4: any; }
129+
>x4 : any
130+
><any>undefined : any
131+
>undefined : undefined
132+
>y4 : any
133+
>{ y4: <any>null } : { y4: any; }
134+
>y4 : any
135+
><any>null : any
136+
>null : null
137+

tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ var [a2]: [any], {b2}: { b2: any }, c2: any, d2: any;
77

88
var {b3}: { b3 }, c3: { b3 }; // error in type instead
99

10-
var [a1] = [undefined], {b1} = { b1: null }, c1 = undefined, d1 = null; // error
10+
var [a4] = [undefined], {b4} = { b4: null }, c4 = undefined, d4 = null; // error
11+
12+
var [a5 = undefined] = []; // error

0 commit comments

Comments
 (0)