|
1 | 1 | //// [defaultParameterAddsUndefinedWithStrictNullChecks.ts]
|
2 | 2 | function f(addUndefined1 = "J", addUndefined2?: number) {
|
| 3 | + return addUndefined1.length + (addUndefined2 || 0); |
3 | 4 | }
|
| 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" |
4 | 30 |
|
5 | 31 |
|
6 | 32 | //// [defaultParameterAddsUndefinedWithStrictNullChecks.js]
|
7 | 33 | function f(addUndefined1, addUndefined2) {
|
8 | 34 | 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; |
9 | 40 | }
|
| 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; |
0 commit comments