More tests of initialised parameters adding undefined
This commit is contained in:
parent
604631881c
commit
739c0838e0
3 changed files with 61 additions and 3 deletions
|
@ -1,9 +1,68 @@
|
|||
//// [defaultParameterAddsUndefinedWithStrictNullChecks.ts]
|
||||
function f(addUndefined1 = "J", addUndefined2?: number) {
|
||||
return addUndefined1.length + (addUndefined2 || 0);
|
||||
}
|
||||
function g(addUndefined = "J", addDefined: number) {
|
||||
return addUndefined.length + addDefined;
|
||||
}
|
||||
let total = f() + f('a', 1) + f('b') + f(undefined, 2);
|
||||
total = g('c', 3) + g(undefined, 4);
|
||||
|
||||
function foo1(x: string = "string", b: number) {
|
||||
x.length;
|
||||
}
|
||||
|
||||
function foo2(x: string | undefined = "string", b: number) {
|
||||
x.length; // ok, should be narrowed to string
|
||||
}
|
||||
|
||||
function foo3(x = "string", b: number) {
|
||||
x.length; // ok, should be narrowed to string
|
||||
}
|
||||
|
||||
foo1(undefined, 1);
|
||||
foo2(undefined, 1);
|
||||
foo3(undefined, 1);
|
||||
|
||||
|
||||
// .d.ts should have `T | undefined` for all of them
|
||||
// need to remove special-case code to allow calling foo1(undefined) for x: string = "string"
|
||||
|
||||
|
||||
//// [defaultParameterAddsUndefinedWithStrictNullChecks.js]
|
||||
function f(addUndefined1, addUndefined2) {
|
||||
if (addUndefined1 === void 0) { addUndefined1 = "J"; }
|
||||
return addUndefined1.length + (addUndefined2 || 0);
|
||||
}
|
||||
function g(addUndefined, addDefined) {
|
||||
if (addUndefined === void 0) { addUndefined = "J"; }
|
||||
return addUndefined.length + addDefined;
|
||||
}
|
||||
var total = f() + f('a', 1) + f('b') + f(undefined, 2);
|
||||
total = g('c', 3) + g(undefined, 4);
|
||||
function foo1(x, b) {
|
||||
if (x === void 0) { x = "string"; }
|
||||
x.length;
|
||||
}
|
||||
function foo2(x, b) {
|
||||
if (x === void 0) { x = "string"; }
|
||||
x.length; // ok, should be narrowed to string
|
||||
}
|
||||
function foo3(x, b) {
|
||||
if (x === void 0) { x = "string"; }
|
||||
x.length; // ok, should be narrowed to string
|
||||
}
|
||||
foo1(undefined, 1);
|
||||
foo2(undefined, 1);
|
||||
foo3(undefined, 1);
|
||||
// .d.ts should have `T | undefined` for all of them
|
||||
// need to remove special-case code to allow calling foo1(undefined) for x: string = "string"
|
||||
|
||||
|
||||
//// [defaultParameterAddsUndefinedWithStrictNullChecks.d.ts]
|
||||
declare function f(addUndefined1?: string | undefined, addUndefined2?: number): number;
|
||||
declare function g(addUndefined: string | undefined, addDefined: number): number;
|
||||
declare let total: number;
|
||||
declare function foo1(x: string | undefined, b: number): void;
|
||||
declare function foo2(x: string | undefined, b: number): void;
|
||||
declare function foo3(x: string | undefined, b: number): void;
|
||||
|
|
|
@ -8,7 +8,7 @@ declare function f1({ a, b }?: { a: number, b: string }): void;
|
|||
>b : string
|
||||
|
||||
function f2({ a, b }: { a: number, b: number } = { a: 0, b: 0 }) {
|
||||
>f2 : ({a, b}?: { a: number; b: number; }) => void
|
||||
>f2 : ({a, b}?: { a: number; b: number; } | undefined) => void
|
||||
>a : number
|
||||
>b : number
|
||||
>a : number
|
||||
|
|
|
@ -26,5 +26,4 @@ foo2(undefined, 1);
|
|||
foo3(undefined, 1);
|
||||
|
||||
|
||||
// .d.ts should have `T | undefined` for all of them
|
||||
// need to remove special-case code to allow calling foo1(undefined) for x: string = "string"
|
||||
// .d.ts should have `T | undefined` for foo1, foo2, foo3
|
||||
|
|
Loading…
Reference in a new issue