Skip to content

Commit 739c083

Browse files
committed
More tests of initialised parameters adding undefined
1 parent 6046318 commit 739c083

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,68 @@
11
//// [defaultParameterAddsUndefinedWithStrictNullChecks.ts]
22
function f(addUndefined1 = "J", addUndefined2?: number) {
3+
return addUndefined1.length + (addUndefined2 || 0);
34
}
5+
function g(addUndefined = "J", addDefined: number) {
6+
return addUndefined.length + addDefined;
7+
}
8+
let total = f() + f('a', 1) + f('b') + f(undefined, 2);
9+
total = g('c', 3) + g(undefined, 4);
10+
11+
function foo1(x: string = "string", b: number) {
12+
x.length;
13+
}
14+
15+
function foo2(x: string | undefined = "string", b: number) {
16+
x.length; // ok, should be narrowed to string
17+
}
18+
19+
function foo3(x = "string", b: number) {
20+
x.length; // ok, should be narrowed to string
21+
}
22+
23+
foo1(undefined, 1);
24+
foo2(undefined, 1);
25+
foo3(undefined, 1);
26+
27+
28+
// .d.ts should have `T | undefined` for all of them
29+
// need to remove special-case code to allow calling foo1(undefined) for x: string = "string"
430

531

632
//// [defaultParameterAddsUndefinedWithStrictNullChecks.js]
733
function f(addUndefined1, addUndefined2) {
834
if (addUndefined1 === void 0) { addUndefined1 = "J"; }
35+
return addUndefined1.length + (addUndefined2 || 0);
36+
}
37+
function g(addUndefined, addDefined) {
38+
if (addUndefined === void 0) { addUndefined = "J"; }
39+
return addUndefined.length + addDefined;
940
}
41+
var total = f() + f('a', 1) + f('b') + f(undefined, 2);
42+
total = g('c', 3) + g(undefined, 4);
43+
function foo1(x, b) {
44+
if (x === void 0) { x = "string"; }
45+
x.length;
46+
}
47+
function foo2(x, b) {
48+
if (x === void 0) { x = "string"; }
49+
x.length; // ok, should be narrowed to string
50+
}
51+
function foo3(x, b) {
52+
if (x === void 0) { x = "string"; }
53+
x.length; // ok, should be narrowed to string
54+
}
55+
foo1(undefined, 1);
56+
foo2(undefined, 1);
57+
foo3(undefined, 1);
58+
// .d.ts should have `T | undefined` for all of them
59+
// need to remove special-case code to allow calling foo1(undefined) for x: string = "string"
60+
61+
62+
//// [defaultParameterAddsUndefinedWithStrictNullChecks.d.ts]
63+
declare function f(addUndefined1?: string | undefined, addUndefined2?: number): number;
64+
declare function g(addUndefined: string | undefined, addDefined: number): number;
65+
declare let total: number;
66+
declare function foo1(x: string | undefined, b: number): void;
67+
declare function foo2(x: string | undefined, b: number): void;
68+
declare function foo3(x: string | undefined, b: number): void;

tests/baselines/reference/destructureOptionalParameter.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ declare function f1({ a, b }?: { a: number, b: string }): void;
88
>b : string
99

1010
function f2({ a, b }: { a: number, b: number } = { a: 0, b: 0 }) {
11-
>f2 : ({a, b}?: { a: number; b: number; }) => void
11+
>f2 : ({a, b}?: { a: number; b: number; } | undefined) => void
1212
>a : number
1313
>b : number
1414
>a : number

tests/cases/compiler/defaultParameterAddsUndefinedWithStrictNullChecks.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,4 @@ foo2(undefined, 1);
2626
foo3(undefined, 1);
2727

2828

29-
// .d.ts should have `T | undefined` for all of them
30-
// need to remove special-case code to allow calling foo1(undefined) for x: string = "string"
29+
// .d.ts should have `T | undefined` for foo1, foo2, foo3

0 commit comments

Comments
 (0)