Skip to content

Commit 3b1309d

Browse files
committed
Test parameter initialisation narrowing rules
1 parent 6543048 commit 3b1309d

4 files changed

+130
-5
lines changed

tests/baselines/reference/defaultParameterAddsUndefinedWithStrictNullChecks.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,27 @@ function foo3(x = "string", b: number) {
2020
x.length; // ok, should be narrowed to string
2121
}
2222

23+
// .d.ts should have `T | undefined` for foo1, foo2, foo3
2324
foo1(undefined, 1);
2425
foo2(undefined, 1);
2526
foo3(undefined, 1);
2627

2728

28-
// .d.ts should have `T | undefined` for foo1, foo2, foo3
29+
function removeUndefinedButNotFalse(x = true) {
30+
if (x === false) {
31+
return x;
32+
}
33+
}
34+
35+
declare const cond: boolean;
36+
function removeNothing(y = cond ? true : undefined) {
37+
if (y !== undefined) {
38+
if (y === false) {
39+
return y;
40+
}
41+
}
42+
return true;
43+
}
2944

3045

3146
//// [defaultParameterAddsUndefinedWithStrictNullChecks.js]
@@ -51,10 +66,25 @@ function foo3(x, b) {
5166
if (x === void 0) { x = "string"; }
5267
x.length; // ok, should be narrowed to string
5368
}
69+
// .d.ts should have `T | undefined` for foo1, foo2, foo3
5470
foo1(undefined, 1);
5571
foo2(undefined, 1);
5672
foo3(undefined, 1);
57-
// .d.ts should have `T | undefined` for foo1, foo2, foo3
73+
function removeUndefinedButNotFalse(x) {
74+
if (x === void 0) { x = true; }
75+
if (x === false) {
76+
return x;
77+
}
78+
}
79+
function removeNothing(y) {
80+
if (y === void 0) { y = cond ? true : undefined; }
81+
if (y !== undefined) {
82+
if (y === false) {
83+
return y;
84+
}
85+
}
86+
return true;
87+
}
5888

5989

6090
//// [defaultParameterAddsUndefinedWithStrictNullChecks.d.ts]
@@ -64,3 +94,6 @@ declare let total: number;
6494
declare function foo1(x: string | undefined, b: number): void;
6595
declare function foo2(x: string | undefined, b: number): void;
6696
declare function foo3(x: string | undefined, b: number): void;
97+
declare function removeUndefinedButNotFalse(x?: boolean | undefined): false | undefined;
98+
declare const cond: boolean;
99+
declare function removeNothing(y?: boolean | undefined): boolean;

tests/baselines/reference/defaultParameterAddsUndefinedWithStrictNullChecks.symbols

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ function foo3(x = "string", b: number) {
6868
>length : Symbol(String.length, Decl(lib.d.ts, --, --))
6969
}
7070

71+
// .d.ts should have `T | undefined` for foo1, foo2, foo3
7172
foo1(undefined, 1);
7273
>foo1 : Symbol(foo1, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 7, 36))
7374
>undefined : Symbol(undefined)
@@ -81,5 +82,38 @@ foo3(undefined, 1);
8182
>undefined : Symbol(undefined)
8283

8384

84-
// .d.ts should have `T | undefined` for foo1, foo2, foo3
85+
function removeUndefinedButNotFalse(x = true) {
86+
>removeUndefinedButNotFalse : Symbol(removeUndefinedButNotFalse, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 24, 19))
87+
>x : Symbol(x, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 27, 36))
88+
89+
if (x === false) {
90+
>x : Symbol(x, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 27, 36))
91+
92+
return x;
93+
>x : Symbol(x, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 27, 36))
94+
}
95+
}
96+
97+
declare const cond: boolean;
98+
>cond : Symbol(cond, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 33, 13))
99+
100+
function removeNothing(y = cond ? true : undefined) {
101+
>removeNothing : Symbol(removeNothing, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 33, 28))
102+
>y : Symbol(y, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 34, 23))
103+
>cond : Symbol(cond, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 33, 13))
104+
>undefined : Symbol(undefined)
105+
106+
if (y !== undefined) {
107+
>y : Symbol(y, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 34, 23))
108+
>undefined : Symbol(undefined)
109+
110+
if (y === false) {
111+
>y : Symbol(y, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 34, 23))
112+
113+
return y;
114+
>y : Symbol(y, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 34, 23))
115+
}
116+
}
117+
return true;
118+
}
85119

tests/baselines/reference/defaultParameterAddsUndefinedWithStrictNullChecks.types

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ function foo3(x = "string", b: number) {
9696
>length : number
9797
}
9898

99+
// .d.ts should have `T | undefined` for foo1, foo2, foo3
99100
foo1(undefined, 1);
100101
>foo1(undefined, 1) : void
101102
>foo1 : (x: string | undefined, b: number) => void
@@ -115,5 +116,47 @@ foo3(undefined, 1);
115116
>1 : 1
116117

117118

118-
// .d.ts should have `T | undefined` for foo1, foo2, foo3
119+
function removeUndefinedButNotFalse(x = true) {
120+
>removeUndefinedButNotFalse : (x?: boolean | undefined) => false | undefined
121+
>x : boolean | undefined
122+
>true : true
123+
124+
if (x === false) {
125+
>x === false : boolean
126+
>x : boolean
127+
>false : false
128+
129+
return x;
130+
>x : false
131+
}
132+
}
133+
134+
declare const cond: boolean;
135+
>cond : boolean
136+
137+
function removeNothing(y = cond ? true : undefined) {
138+
>removeNothing : (y?: boolean | undefined) => boolean
139+
>y : boolean | undefined
140+
>cond ? true : undefined : true | undefined
141+
>cond : boolean
142+
>true : true
143+
>undefined : undefined
144+
145+
if (y !== undefined) {
146+
>y !== undefined : boolean
147+
>y : boolean | undefined
148+
>undefined : undefined
149+
150+
if (y === false) {
151+
>y === false : boolean
152+
>y : boolean
153+
>false : false
154+
155+
return y;
156+
>y : false
157+
}
158+
}
159+
return true;
160+
>true : true
161+
}
119162

tests/cases/compiler/defaultParameterAddsUndefinedWithStrictNullChecks.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,24 @@ function foo3(x = "string", b: number) {
2121
x.length; // ok, should be narrowed to string
2222
}
2323

24+
// .d.ts should have `T | undefined` for foo1, foo2, foo3
2425
foo1(undefined, 1);
2526
foo2(undefined, 1);
2627
foo3(undefined, 1);
2728

2829

29-
// .d.ts should have `T | undefined` for foo1, foo2, foo3
30+
function removeUndefinedButNotFalse(x = true) {
31+
if (x === false) {
32+
return x;
33+
}
34+
}
35+
36+
declare const cond: boolean;
37+
function removeNothing(y = cond ? true : undefined) {
38+
if (y !== undefined) {
39+
if (y === false) {
40+
return y;
41+
}
42+
}
43+
return true;
44+
}

0 commit comments

Comments
 (0)