Skip to content

Commit 5a87a94

Browse files
committed
microsoft#17080 Added testcases from the Github bugreport (all working as intended now). Signed CLA.
1 parent 18e1ac0 commit 5a87a94

6 files changed

+229
-2
lines changed

pull_request_template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Here's a checklist you might find useful.
66
'Bug' or 'help wanted' or is in the Community milestone
77
[X] Code is up-to-date with the `master` branch
88
[X] You've successfully run `jake runtests` locally
9-
[ ] You've signed the CLA
9+
[X] You've signed the CLA
1010
[X] There are new or updated unit tests validating the change
1111
1212
Refer to CONTRIBUTING.MD for more details.

tests/baselines/reference/optionalParameterInDestructuringWithInitializer.errors.txt

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts(21,7): e
66
Type 'undefined' is not assignable to type 'number'.
77
tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts(31,8): error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
88
Type 'undefined' is not assignable to type 'number'.
9+
tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts(45,10): error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
10+
Type 'undefined' is not assignable to type 'number'.
11+
tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts(55,11): error TS2345: Argument of type 'number | null' is not assignable to parameter of type 'number | undefined'.
12+
Type 'null' is not assignable to type 'number | undefined'.
913

1014

11-
==== tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts (4 errors) ====
15+
==== tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts (6 errors) ====
1216
// https://github.com/Microsoft/TypeScript/issues/17080
1317
function f(a:number,b:number) {
1418
}
@@ -59,4 +63,36 @@ tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts(31,8): e
5963
f(b, c)
6064
// no error
6165
}
66+
67+
interface Foo {
68+
readonly bar?: number;
69+
}
70+
71+
function performFoo({ bar }: Foo = {}) {
72+
useBar(bar);
73+
~~~
74+
!!! error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
75+
!!! error TS2345: Type 'undefined' is not assignable to type 'number'.
76+
}
77+
78+
function useBar(bar: number) {
79+
f(bar, 1)
80+
}
81+
82+
performFoo();
83+
84+
function performFoo2({ bar = null }: Foo = {}) {
85+
useBar2(bar);
86+
~~~
87+
!!! error TS2345: Argument of type 'number | null' is not assignable to parameter of type 'number | undefined'.
88+
!!! error TS2345: Type 'null' is not assignable to type 'number | undefined'.
89+
}
90+
91+
function useBar2(bar: number | undefined) {
92+
if (bar) {
93+
f(bar, 1)
94+
}
95+
}
96+
97+
performFoo2();
6298

tests/baselines/reference/optionalParameterInDestructuringWithInitializer.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,32 @@ function func7( {a: {b, c = 6} = {b: 4, c: 5}, d}: {a: {b: number, c?: number},
3737
f(b, c)
3838
// no error
3939
}
40+
41+
interface Foo {
42+
readonly bar?: number;
43+
}
44+
45+
function performFoo({ bar }: Foo = {}) {
46+
useBar(bar);
47+
}
48+
49+
function useBar(bar: number) {
50+
f(bar, 1)
51+
}
52+
53+
performFoo();
54+
55+
function performFoo2({ bar = null }: Foo = {}) {
56+
useBar2(bar);
57+
}
58+
59+
function useBar2(bar: number | undefined) {
60+
if (bar) {
61+
f(bar, 1)
62+
}
63+
}
64+
65+
performFoo2();
4066

4167

4268
//// [optionalParameterInDestructuringWithInitializer.js]
@@ -78,3 +104,21 @@ function func7(_a) {
78104
f(b, c);
79105
// no error
80106
}
107+
function performFoo(_a) {
108+
var bar = (_a === void 0 ? {} : _a).bar;
109+
useBar(bar);
110+
}
111+
function useBar(bar) {
112+
f(bar, 1);
113+
}
114+
performFoo();
115+
function performFoo2(_a) {
116+
var _b = (_a === void 0 ? {} : _a).bar, bar = _b === void 0 ? null : _b;
117+
useBar2(bar);
118+
}
119+
function useBar2(bar) {
120+
if (bar) {
121+
f(bar, 1);
122+
}
123+
}
124+
performFoo2();

tests/baselines/reference/optionalParameterInDestructuringWithInitializer.symbols

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,58 @@ function func7( {a: {b, c = 6} = {b: 4, c: 5}, d}: {a: {b: number, c?: number},
152152
// no error
153153
}
154154

155+
interface Foo {
156+
>Foo : Symbol(Foo, Decl(optionalParameterInDestructuringWithInitializer.ts, 37, 1))
157+
158+
readonly bar?: number;
159+
>bar : Symbol(Foo.bar, Decl(optionalParameterInDestructuringWithInitializer.ts, 39, 15))
160+
}
161+
162+
function performFoo({ bar }: Foo = {}) {
163+
>performFoo : Symbol(performFoo, Decl(optionalParameterInDestructuringWithInitializer.ts, 41, 1))
164+
>bar : Symbol(bar, Decl(optionalParameterInDestructuringWithInitializer.ts, 43, 21))
165+
>Foo : Symbol(Foo, Decl(optionalParameterInDestructuringWithInitializer.ts, 37, 1))
166+
167+
useBar(bar);
168+
>useBar : Symbol(useBar, Decl(optionalParameterInDestructuringWithInitializer.ts, 45, 1))
169+
>bar : Symbol(bar, Decl(optionalParameterInDestructuringWithInitializer.ts, 43, 21))
170+
}
171+
172+
function useBar(bar: number) {
173+
>useBar : Symbol(useBar, Decl(optionalParameterInDestructuringWithInitializer.ts, 45, 1))
174+
>bar : Symbol(bar, Decl(optionalParameterInDestructuringWithInitializer.ts, 47, 16))
175+
176+
f(bar, 1)
177+
>f : Symbol(f, Decl(optionalParameterInDestructuringWithInitializer.ts, 0, 0))
178+
>bar : Symbol(bar, Decl(optionalParameterInDestructuringWithInitializer.ts, 47, 16))
179+
}
180+
181+
performFoo();
182+
>performFoo : Symbol(performFoo, Decl(optionalParameterInDestructuringWithInitializer.ts, 41, 1))
183+
184+
function performFoo2({ bar = null }: Foo = {}) {
185+
>performFoo2 : Symbol(performFoo2, Decl(optionalParameterInDestructuringWithInitializer.ts, 51, 13))
186+
>bar : Symbol(bar, Decl(optionalParameterInDestructuringWithInitializer.ts, 53, 22))
187+
>Foo : Symbol(Foo, Decl(optionalParameterInDestructuringWithInitializer.ts, 37, 1))
188+
189+
useBar2(bar);
190+
>useBar2 : Symbol(useBar2, Decl(optionalParameterInDestructuringWithInitializer.ts, 55, 1))
191+
>bar : Symbol(bar, Decl(optionalParameterInDestructuringWithInitializer.ts, 53, 22))
192+
}
193+
194+
function useBar2(bar: number | undefined) {
195+
>useBar2 : Symbol(useBar2, Decl(optionalParameterInDestructuringWithInitializer.ts, 55, 1))
196+
>bar : Symbol(bar, Decl(optionalParameterInDestructuringWithInitializer.ts, 57, 17))
197+
198+
if (bar) {
199+
>bar : Symbol(bar, Decl(optionalParameterInDestructuringWithInitializer.ts, 57, 17))
200+
201+
f(bar, 1)
202+
>f : Symbol(f, Decl(optionalParameterInDestructuringWithInitializer.ts, 0, 0))
203+
>bar : Symbol(bar, Decl(optionalParameterInDestructuringWithInitializer.ts, 57, 17))
204+
}
205+
}
206+
207+
performFoo2();
208+
>performFoo2 : Symbol(performFoo2, Decl(optionalParameterInDestructuringWithInitializer.ts, 51, 13))
209+

tests/baselines/reference/optionalParameterInDestructuringWithInitializer.types

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,69 @@ function func7( {a: {b, c = 6} = {b: 4, c: 5}, d}: {a: {b: number, c?: number},
196196
// no error
197197
}
198198

199+
interface Foo {
200+
>Foo : Foo
201+
202+
readonly bar?: number;
203+
>bar : number | undefined
204+
}
205+
206+
function performFoo({ bar }: Foo = {}) {
207+
>performFoo : ({ bar }?: Foo) => void
208+
>bar : number | undefined
209+
>Foo : Foo
210+
>{} : {}
211+
212+
useBar(bar);
213+
>useBar(bar) : void
214+
>useBar : (bar: number) => void
215+
>bar : number | undefined
216+
}
217+
218+
function useBar(bar: number) {
219+
>useBar : (bar: number) => void
220+
>bar : number
221+
222+
f(bar, 1)
223+
>f(bar, 1) : void
224+
>f : (a: number, b: number) => void
225+
>bar : number
226+
>1 : 1
227+
}
228+
229+
performFoo();
230+
>performFoo() : void
231+
>performFoo : ({ bar }?: Foo) => void
232+
233+
function performFoo2({ bar = null }: Foo = {}) {
234+
>performFoo2 : ({ bar }?: Foo) => void
235+
>bar : number | null
236+
>null : null
237+
>Foo : Foo
238+
>{} : {}
239+
240+
useBar2(bar);
241+
>useBar2(bar) : void
242+
>useBar2 : (bar: number | undefined) => void
243+
>bar : number | null
244+
}
245+
246+
function useBar2(bar: number | undefined) {
247+
>useBar2 : (bar: number | undefined) => void
248+
>bar : number | undefined
249+
250+
if (bar) {
251+
>bar : number | undefined
252+
253+
f(bar, 1)
254+
>f(bar, 1) : void
255+
>f : (a: number, b: number) => void
256+
>bar : number
257+
>1 : 1
258+
}
259+
}
260+
261+
performFoo2();
262+
>performFoo2() : void
263+
>performFoo2 : ({ bar }?: Foo) => void
264+

tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,29 @@ function func7( {a: {b, c = 6} = {b: 4, c: 5}, d}: {a: {b: number, c?: number},
3737
f(b, c)
3838
// no error
3939
}
40+
41+
interface Foo {
42+
readonly bar?: number;
43+
}
44+
45+
function performFoo({ bar }: Foo = {}) {
46+
useBar(bar);
47+
}
48+
49+
function useBar(bar: number) {
50+
f(bar, 1)
51+
}
52+
53+
performFoo();
54+
55+
function performFoo2({ bar = null }: Foo = {}) {
56+
useBar2(bar);
57+
}
58+
59+
function useBar2(bar: number | undefined) {
60+
if (bar) {
61+
f(bar, 1)
62+
}
63+
}
64+
65+
performFoo2();

0 commit comments

Comments
 (0)