Skip to content

Commit 90d347e

Browse files
committed
Do not report errors during contextual typecheck
Fixes #8229
1 parent dfee3de commit 90d347e

File tree

4 files changed

+230
-76
lines changed

4 files changed

+230
-76
lines changed

src/compiler/checker.ts

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

28742874
// If the declaration specifies a binding pattern, use the type implied by the binding pattern
28752875
if (isBindingPattern(declaration.name)) {
2876-
return getTypeFromBindingPattern(<BindingPattern>declaration.name, /*includePatternInType*/ false);
2876+
return getTypeFromBindingPattern(<BindingPattern>declaration.name, /*includePatternInType*/ false, /*reportErrors*/ true);
28772877
}
28782878

28792879
// No type specified and nothing can be inferred
@@ -2883,23 +2883,25 @@ namespace ts {
28832883
// Return the type implied by a binding pattern element. This is the type of the initializer of the element if
28842884
// one is present. Otherwise, if the element is itself a binding pattern, it is the type implied by the binding
28852885
// pattern. Otherwise, it is the type any.
2886-
function getTypeFromBindingElement(element: BindingElement, includePatternInType?: boolean): Type {
2886+
function getTypeFromBindingElement(element: BindingElement, includePatternInType?: boolean, reportErrors?: boolean): Type {
28872887
if (element.initializer) {
28882888
const type = checkExpressionCached(element.initializer);
2889-
reportErrorsFromWidening(element, type);
2889+
if (reportErrors) {
2890+
reportErrorsFromWidening(element, type);
2891+
}
28902892
return getWidenedType(type);
28912893
}
28922894
if (isBindingPattern(element.name)) {
2893-
return getTypeFromBindingPattern(<BindingPattern>element.name, includePatternInType);
2895+
return getTypeFromBindingPattern(<BindingPattern>element.name, includePatternInType, reportErrors);
28942896
}
2895-
if (compilerOptions.noImplicitAny && !declarationBelongsToPrivateAmbientMember(element)) {
2897+
if (reportErrors && compilerOptions.noImplicitAny && !declarationBelongsToPrivateAmbientMember(element)) {
28962898
reportImplicitAnyError(element, anyType);
28972899
}
28982900
return anyType;
28992901
}
29002902

29012903
// Return the type implied by an object binding pattern
2902-
function getTypeFromObjectBindingPattern(pattern: BindingPattern, includePatternInType: boolean): Type {
2904+
function getTypeFromObjectBindingPattern(pattern: BindingPattern, includePatternInType: boolean, reportErrors: boolean): Type {
29032905
const members: SymbolTable = {};
29042906
let hasComputedProperties = false;
29052907
forEach(pattern.elements, e => {
@@ -2913,7 +2915,7 @@ namespace ts {
29132915
const text = getTextOfPropertyName(name);
29142916
const flags = SymbolFlags.Property | SymbolFlags.Transient | (e.initializer ? SymbolFlags.Optional : 0);
29152917
const symbol = <TransientSymbol>createSymbol(flags, text);
2916-
symbol.type = getTypeFromBindingElement(e, includePatternInType);
2918+
symbol.type = getTypeFromBindingElement(e, includePatternInType, reportErrors);
29172919
symbol.bindingElement = e;
29182920
members[symbol.name] = symbol;
29192921
});
@@ -2928,13 +2930,13 @@ namespace ts {
29282930
}
29292931

29302932
// Return the type implied by an array binding pattern
2931-
function getTypeFromArrayBindingPattern(pattern: BindingPattern, includePatternInType: boolean): Type {
2933+
function getTypeFromArrayBindingPattern(pattern: BindingPattern, includePatternInType: boolean, reportErrors: boolean): Type {
29322934
const elements = pattern.elements;
29332935
if (elements.length === 0 || elements[elements.length - 1].dotDotDotToken) {
29342936
return languageVersion >= ScriptTarget.ES6 ? createIterableType(anyType) : anyArrayType;
29352937
}
29362938
// If the pattern has at least one element, and no rest element, then it should imply a tuple type.
2937-
const elementTypes = map(elements, e => e.kind === SyntaxKind.OmittedExpression ? anyType : getTypeFromBindingElement(e, includePatternInType));
2939+
const elementTypes = map(elements, e => e.kind === SyntaxKind.OmittedExpression ? anyType : getTypeFromBindingElement(e, includePatternInType, reportErrors));
29382940
if (includePatternInType) {
29392941
const result = createNewTupleType(elementTypes);
29402942
result.pattern = pattern;
@@ -2950,10 +2952,10 @@ namespace ts {
29502952
// used as the contextual type of an initializer associated with the binding pattern. Also, for a destructuring
29512953
// parameter with no type annotation or initializer, the type implied by the binding pattern becomes the type of
29522954
// the parameter.
2953-
function getTypeFromBindingPattern(pattern: BindingPattern, includePatternInType?: boolean): Type {
2955+
function getTypeFromBindingPattern(pattern: BindingPattern, includePatternInType?: boolean, reportErrors?: boolean): Type {
29542956
return pattern.kind === SyntaxKind.ObjectBindingPattern
2955-
? getTypeFromObjectBindingPattern(pattern, includePatternInType)
2956-
: getTypeFromArrayBindingPattern(pattern, includePatternInType);
2957+
? getTypeFromObjectBindingPattern(pattern, includePatternInType, reportErrors)
2958+
: getTypeFromArrayBindingPattern(pattern, includePatternInType, reportErrors);
29572959
}
29582960

29592961
// Return the type associated with a variable, parameter, or property declaration. In the simple case this is the type
@@ -8467,7 +8469,7 @@ namespace ts {
84678469
}
84688470
}
84698471
if (isBindingPattern(declaration.name)) {
8470-
return getTypeFromBindingPattern(<BindingPattern>declaration.name, /*includePatternInType*/ true);
8472+
return getTypeFromBindingPattern(<BindingPattern>declaration.name, /*includePatternInType*/ true, /*reportErrors*/ false);
84718473
}
84728474
if (isBindingPattern(declaration.parent)) {
84738475
const parentDeclaration = declaration.parent.parent;

tests/baselines/reference/noImplicitAnyDestructuringVarDeclaration2.errors.txt

Lines changed: 0 additions & 63 deletions
This file was deleted.
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+

0 commit comments

Comments
 (0)