Skip to content

Commit 8e4541e

Browse files
committed
Merge pull request #7969 from Microsoft/bindingPatternImplicitAny
Report implicit any errors on destructuring declarations
2 parents a7bf690 + 1d27307 commit 8e4541e

10 files changed

+216
-6
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2836,11 +2836,16 @@ namespace ts {
28362836
// pattern. Otherwise, it is the type any.
28372837
function getTypeFromBindingElement(element: BindingElement, includePatternInType?: boolean): Type {
28382838
if (element.initializer) {
2839-
return getWidenedType(checkExpressionCached(element.initializer));
2839+
const type = checkExpressionCached(element.initializer);
2840+
reportErrorsFromWidening(element, type);
2841+
return getWidenedType(type);
28402842
}
28412843
if (isBindingPattern(element.name)) {
28422844
return getTypeFromBindingPattern(<BindingPattern>element.name, includePatternInType);
28432845
}
2846+
if (compilerOptions.noImplicitAny) {
2847+
reportImplicitAnyError(element, anyType);
2848+
}
28442849
return anyType;
28452850
}
28462851

@@ -6799,6 +6804,9 @@ namespace ts {
67996804
Diagnostics.Rest_parameter_0_implicitly_has_an_any_type :
68006805
Diagnostics.Parameter_0_implicitly_has_an_1_type;
68016806
break;
6807+
case SyntaxKind.BindingElement:
6808+
diagnostic = Diagnostics.Binding_element_0_implicitly_has_an_1_type;
6809+
break;
68026810
case SyntaxKind.FunctionDeclaration:
68036811
case SyntaxKind.MethodDeclaration:
68046812
case SyntaxKind.MethodSignature:

src/compiler/declarationEmitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace ts {
3535
forEachExpectedEmitFile(host, getDeclarationDiagnosticsFromFile, targetSourceFile);
3636
return declarationDiagnostics.getDiagnostics(targetSourceFile ? targetSourceFile.fileName : undefined);
3737

38-
function getDeclarationDiagnosticsFromFile({ declarationFilePath }, sources: SourceFile[], isBundledEmit: boolean) {
38+
function getDeclarationDiagnosticsFromFile({ declarationFilePath }: EmitFileNames, sources: SourceFile[], isBundledEmit: boolean) {
3939
emitDeclarations(host, resolver, declarationDiagnostics, declarationFilePath, sources, isBundledEmit);
4040
}
4141
}

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2752,6 +2752,10 @@
27522752
"category": "Error",
27532753
"code": 7030
27542754
},
2755+
"Binding element '{0}' implicitly has an '{1}' type.": {
2756+
"category": "Error",
2757+
"code": 7031
2758+
},
27552759
"You cannot rename this element.": {
27562760
"category": "Error",
27572761
"code": 8000
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
tests/cases/compiler/noImplicitAnyDestructuringParameterDeclaration.ts(1,14): error TS7031: Binding element 'a' implicitly has an 'any' type.
2+
tests/cases/compiler/noImplicitAnyDestructuringParameterDeclaration.ts(1,19): error TS7031: Binding element 'b' implicitly has an 'any' type.
3+
tests/cases/compiler/noImplicitAnyDestructuringParameterDeclaration.ts(1,23): error TS7006: Parameter 'c' implicitly has an 'any' type.
4+
tests/cases/compiler/noImplicitAnyDestructuringParameterDeclaration.ts(1,26): error TS7006: Parameter 'd' implicitly has an 'any' type.
5+
tests/cases/compiler/noImplicitAnyDestructuringParameterDeclaration.ts(3,14): error TS7031: Binding element 'a' implicitly has an 'any' type.
6+
tests/cases/compiler/noImplicitAnyDestructuringParameterDeclaration.ts(3,31): error TS7031: Binding element 'b' implicitly has an 'any' type.
7+
tests/cases/compiler/noImplicitAnyDestructuringParameterDeclaration.ts(3,42): error TS7006: Parameter 'c' implicitly has an 'any' type.
8+
tests/cases/compiler/noImplicitAnyDestructuringParameterDeclaration.ts(3,57): error TS7006: Parameter 'd' implicitly has an 'any' type.
9+
tests/cases/compiler/noImplicitAnyDestructuringParameterDeclaration.ts(7,20): error TS7008: Member 'b' implicitly has an 'any' type.
10+
tests/cases/compiler/noImplicitAnyDestructuringParameterDeclaration.ts(7,30): error TS7008: Member 'b' implicitly has an 'any' type.
11+
tests/cases/compiler/noImplicitAnyDestructuringParameterDeclaration.ts(9,14): error TS7031: Binding element 'a1' implicitly has an 'any' type.
12+
tests/cases/compiler/noImplicitAnyDestructuringParameterDeclaration.ts(9,34): error TS7031: Binding element 'b1' implicitly has an 'any' type.
13+
tests/cases/compiler/noImplicitAnyDestructuringParameterDeclaration.ts(9,54): error TS7006: Parameter 'c1' implicitly has an 'any' type.
14+
tests/cases/compiler/noImplicitAnyDestructuringParameterDeclaration.ts(9,70): error TS7006: Parameter 'd1' implicitly has an 'any' type.
15+
16+
17+
==== tests/cases/compiler/noImplicitAnyDestructuringParameterDeclaration.ts (14 errors) ====
18+
function f1([a], {b}, c, d) { // error
19+
~
20+
!!! error TS7031: Binding element 'a' implicitly has an 'any' type.
21+
~
22+
!!! error TS7031: Binding element 'b' implicitly has an 'any' type.
23+
~
24+
!!! error TS7006: Parameter 'c' implicitly has an 'any' type.
25+
~
26+
!!! error TS7006: Parameter 'd' implicitly has an 'any' type.
27+
}
28+
function f2([a = undefined], {b = null}, c = undefined, d = null) { // error
29+
~
30+
!!! error TS7031: Binding element 'a' implicitly has an 'any' type.
31+
~
32+
!!! error TS7031: Binding element 'b' implicitly has an 'any' type.
33+
~~~~~~~~~~~~~
34+
!!! error TS7006: Parameter 'c' implicitly has an 'any' type.
35+
~~~~~~~~
36+
!!! error TS7006: Parameter 'd' implicitly has an 'any' type.
37+
}
38+
function f3([a]: [any], {b}: { b: any }, c: any, d: any) {
39+
}
40+
function f4({b}: { b }, x: { b }) { // error in type instead
41+
~
42+
!!! error TS7008: Member 'b' implicitly has an 'any' type.
43+
~
44+
!!! error TS7008: Member 'b' implicitly has an 'any' type.
45+
}
46+
function f5([a1] = [undefined], {b1} = { b1: null }, c1 = undefined, d1 = null) { // error
47+
~~
48+
!!! error TS7031: Binding element 'a1' implicitly has an 'any' type.
49+
~~
50+
!!! error TS7031: Binding element 'b1' implicitly has an 'any' type.
51+
~~~~~~~~~~~~~~
52+
!!! error TS7006: Parameter 'c1' implicitly has an 'any' type.
53+
~~~~~~~~~
54+
!!! error TS7006: Parameter 'd1' implicitly has an 'any' type.
55+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [noImplicitAnyDestructuringParameterDeclaration.ts]
2+
function f1([a], {b}, c, d) { // error
3+
}
4+
function f2([a = undefined], {b = null}, c = undefined, d = null) { // error
5+
}
6+
function f3([a]: [any], {b}: { b: any }, c: any, d: any) {
7+
}
8+
function f4({b}: { b }, x: { b }) { // error in type instead
9+
}
10+
function f5([a1] = [undefined], {b1} = { b1: null }, c1 = undefined, d1 = null) { // error
11+
}
12+
13+
//// [noImplicitAnyDestructuringParameterDeclaration.js]
14+
function f1(_a, _b, c, d) {
15+
var a = _a[0];
16+
var b = _b.b;
17+
}
18+
function f2(_a, _b, c, d) {
19+
var _c = _a[0], a = _c === void 0 ? undefined : _c;
20+
var _d = _b.b, b = _d === void 0 ? null : _d;
21+
if (c === void 0) { c = undefined; }
22+
if (d === void 0) { d = null; }
23+
}
24+
function f3(_a, _b, c, d) {
25+
var a = _a[0];
26+
var b = _b.b;
27+
}
28+
function f4(_a, x) {
29+
var b = _a.b;
30+
}
31+
function f5(_a, _b, c1, d1) {
32+
var a1 = (_a === void 0 ? [undefined] : _a)[0];
33+
var b1 = (_b === void 0 ? { b1: null } : _b).b1;
34+
if (c1 === void 0) { c1 = undefined; }
35+
if (d1 === void 0) { d1 = null; }
36+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(1,5): error TS1182: A destructuring declaration must have an initializer.
2+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(1,6): error TS7031: Binding element 'a' implicitly has an 'any' type.
3+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(1,10): error TS1182: A destructuring declaration must have an initializer.
4+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(1,11): error TS7031: Binding element 'b' implicitly has an 'any' type.
5+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(1,15): error TS7005: Variable 'c' implicitly has an 'any' type.
6+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(1,18): error TS7005: Variable 'd' implicitly has an 'any' type.
7+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(3,5): error TS1182: A destructuring declaration must have an initializer.
8+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(3,6): error TS7031: Binding element 'a1' implicitly has an 'any' type.
9+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(3,23): error TS1182: A destructuring declaration must have an initializer.
10+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(3,24): error TS7031: Binding element 'b1' implicitly has an 'any' type.
11+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(3,36): error TS7005: Variable 'c1' implicitly has an 'any' type.
12+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(3,52): error TS7005: Variable 'd1' implicitly has an 'any' type.
13+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(5,5): error TS1182: A destructuring declaration must have an initializer.
14+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(5,18): error TS1182: A destructuring declaration must have an initializer.
15+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(7,5): error TS1182: A destructuring declaration must have an initializer.
16+
tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts(7,13): error TS7008: Member 'b3' implicitly has an 'any' type.
17+
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.
20+
21+
22+
==== tests/cases/compiler/noImplicitAnyDestructuringVarDeclaration.ts (19 errors) ====
23+
var [a], {b}, c, d; // error
24+
~~~
25+
!!! error TS1182: A destructuring declaration must have an initializer.
26+
~
27+
!!! error TS7031: Binding element 'a' implicitly has an 'any' type.
28+
~~~
29+
!!! error TS1182: A destructuring declaration must have an initializer.
30+
~
31+
!!! error TS7031: Binding element 'b' implicitly has an 'any' type.
32+
~
33+
!!! error TS7005: Variable 'c' implicitly has an 'any' type.
34+
~
35+
!!! error TS7005: Variable 'd' implicitly has an 'any' type.
36+
37+
var [a1 = undefined], {b1 = null}, c1 = undefined, d1 = null; // error
38+
~~~~~~~~~~~~~~~~
39+
!!! error TS1182: A destructuring declaration must have an initializer.
40+
~~
41+
!!! error TS7031: Binding element 'a1' implicitly has an 'any' type.
42+
~~~~~~~~~~~
43+
!!! error TS1182: A destructuring declaration must have an initializer.
44+
~~
45+
!!! error TS7031: Binding element 'b1' implicitly has an 'any' type.
46+
~~
47+
!!! error TS7005: Variable 'c1' implicitly has an 'any' type.
48+
~~
49+
!!! error TS7005: Variable 'd1' implicitly has an 'any' type.
50+
51+
var [a2]: [any], {b2}: { b2: any }, c2: any, d2: any;
52+
~~~~
53+
!!! error TS1182: A destructuring declaration must have an initializer.
54+
~~~~
55+
!!! error TS1182: A destructuring declaration must have an initializer.
56+
57+
var {b3}: { b3 }, c3: { b3 }; // error in type instead
58+
~~~~
59+
!!! error TS1182: A destructuring declaration must have an initializer.
60+
~~
61+
!!! error TS7008: Member 'b3' implicitly has an 'any' type.
62+
~~
63+
!!! error TS7008: Member 'b3' implicitly has an 'any' type.
64+
65+
var [a1] = [undefined], {b1} = { b1: null }, c1 = undefined, d1 = null; // error
66+
~~
67+
!!! error TS7031: Binding element 'a1' implicitly has an 'any' type.
68+
~~
69+
!!! error TS7031: Binding element 'b1' implicitly has an 'any' type.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [noImplicitAnyDestructuringVarDeclaration.ts]
2+
var [a], {b}, c, d; // error
3+
4+
var [a1 = undefined], {b1 = null}, c1 = undefined, d1 = null; // error
5+
6+
var [a2]: [any], {b2}: { b2: any }, c2: any, d2: any;
7+
8+
var {b3}: { b3 }, c3: { b3 }; // error in type instead
9+
10+
var [a1] = [undefined], {b1} = { b1: null }, c1 = undefined, d1 = null; // error
11+
12+
//// [noImplicitAnyDestructuringVarDeclaration.js]
13+
var a = (void 0)[0], b = (void 0).b, c, d; // error
14+
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
15+
var a2 = (void 0)[0], b2 = (void 0).b2, c2, d2;
16+
var b3 = (void 0).b3, c3; // error in type instead
17+
var a1 = [undefined][0], b1 = { b1: null }.b1, c1 = undefined, d1 = null; // error
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
tests/cases/conformance/types/tuple/wideningTuples5.ts(1,6): error TS7005: Variable 'a' implicitly has an 'any' type.
2-
tests/cases/conformance/types/tuple/wideningTuples5.ts(1,9): error TS7005: Variable 'b' implicitly has an 'any' type.
1+
tests/cases/conformance/types/tuple/wideningTuples5.ts(1,6): error TS7031: Binding element 'a' implicitly has an 'any' type.
2+
tests/cases/conformance/types/tuple/wideningTuples5.ts(1,9): error TS7031: Binding element 'b' implicitly has an 'any' type.
33

44

55
==== tests/cases/conformance/types/tuple/wideningTuples5.ts (2 errors) ====
66
var [a, b] = [undefined, null];
77
~
8-
!!! error TS7005: Variable 'a' implicitly has an 'any' type.
8+
!!! error TS7031: Binding element 'a' implicitly has an 'any' type.
99
~
10-
!!! error TS7005: Variable 'b' implicitly has an 'any' type.
10+
!!! error TS7031: Binding element 'b' implicitly has an 'any' type.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @noimplicitany: true
2+
function f1([a], {b}, c, d) { // error
3+
}
4+
function f2([a = undefined], {b = null}, c = undefined, d = null) { // error
5+
}
6+
function f3([a]: [any], {b}: { b: any }, c: any, d: any) {
7+
}
8+
function f4({b}: { b }, x: { b }) { // error in type instead
9+
}
10+
function f5([a1] = [undefined], {b1} = { b1: null }, c1 = undefined, d1 = null) { // error
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @noimplicitany: true
2+
var [a], {b}, c, d; // error
3+
4+
var [a1 = undefined], {b1 = null}, c1 = undefined, d1 = null; // error
5+
6+
var [a2]: [any], {b2}: { b2: any }, c2: any, d2: any;
7+
8+
var {b3}: { b3 }, c3: { b3 }; // error in type instead
9+
10+
var [a1] = [undefined], {b1} = { b1: null }, c1 = undefined, d1 = null; // error

0 commit comments

Comments
 (0)