More tests of parameter initialiser type
This commit is contained in:
parent
bb6f3ad29a
commit
f097eafd11
3 changed files with 192 additions and 8 deletions
|
@ -3,5 +3,84 @@ function f(addUndefined1 = "J", addUndefined2?: number) {
|
|||
>f : Symbol(f, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 0, 0))
|
||||
>addUndefined1 : Symbol(addUndefined1, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 0, 11))
|
||||
>addUndefined2 : Symbol(addUndefined2, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 0, 31))
|
||||
|
||||
return addUndefined1.length + (addUndefined2 || 0);
|
||||
>addUndefined1.length : Symbol(String.length, Decl(lib.d.ts, --, --))
|
||||
>addUndefined1 : Symbol(addUndefined1, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 0, 11))
|
||||
>length : Symbol(String.length, Decl(lib.d.ts, --, --))
|
||||
>addUndefined2 : Symbol(addUndefined2, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 0, 31))
|
||||
}
|
||||
function g(addUndefined = "J", addDefined: number) {
|
||||
>g : Symbol(g, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 2, 1))
|
||||
>addUndefined : Symbol(addUndefined, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 3, 11))
|
||||
>addDefined : Symbol(addDefined, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 3, 30))
|
||||
|
||||
return addUndefined.length + addDefined;
|
||||
>addUndefined.length : Symbol(String.length, Decl(lib.d.ts, --, --))
|
||||
>addUndefined : Symbol(addUndefined, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 3, 11))
|
||||
>length : Symbol(String.length, Decl(lib.d.ts, --, --))
|
||||
>addDefined : Symbol(addDefined, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 3, 30))
|
||||
}
|
||||
let total = f() + f('a', 1) + f('b') + f(undefined, 2);
|
||||
>total : Symbol(total, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 6, 3))
|
||||
>f : Symbol(f, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 0, 0))
|
||||
>f : Symbol(f, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 0, 0))
|
||||
>f : Symbol(f, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 0, 0))
|
||||
>f : Symbol(f, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 0, 0))
|
||||
>undefined : Symbol(undefined)
|
||||
|
||||
total = g('c', 3) + g(undefined, 4);
|
||||
>total : Symbol(total, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 6, 3))
|
||||
>g : Symbol(g, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 2, 1))
|
||||
>g : Symbol(g, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 2, 1))
|
||||
>undefined : Symbol(undefined)
|
||||
|
||||
function foo1(x: string = "string", b: number) {
|
||||
>foo1 : Symbol(foo1, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 7, 36))
|
||||
>x : Symbol(x, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 9, 14))
|
||||
>b : Symbol(b, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 9, 35))
|
||||
|
||||
x.length;
|
||||
>x.length : Symbol(String.length, Decl(lib.d.ts, --, --))
|
||||
>x : Symbol(x, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 9, 14))
|
||||
>length : Symbol(String.length, Decl(lib.d.ts, --, --))
|
||||
}
|
||||
|
||||
function foo2(x: string | undefined = "string", b: number) {
|
||||
>foo2 : Symbol(foo2, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 11, 1))
|
||||
>x : Symbol(x, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 13, 14))
|
||||
>b : Symbol(b, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 13, 47))
|
||||
|
||||
x.length; // ok, should be narrowed to string
|
||||
>x.length : Symbol(String.length, Decl(lib.d.ts, --, --))
|
||||
>x : Symbol(x, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 13, 14))
|
||||
>length : Symbol(String.length, Decl(lib.d.ts, --, --))
|
||||
}
|
||||
|
||||
function foo3(x = "string", b: number) {
|
||||
>foo3 : Symbol(foo3, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 15, 1))
|
||||
>x : Symbol(x, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 17, 14))
|
||||
>b : Symbol(b, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 17, 27))
|
||||
|
||||
x.length; // ok, should be narrowed to string
|
||||
>x.length : Symbol(String.length, Decl(lib.d.ts, --, --))
|
||||
>x : Symbol(x, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 17, 14))
|
||||
>length : Symbol(String.length, Decl(lib.d.ts, --, --))
|
||||
}
|
||||
|
||||
foo1(undefined, 1);
|
||||
>foo1 : Symbol(foo1, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 7, 36))
|
||||
>undefined : Symbol(undefined)
|
||||
|
||||
foo2(undefined, 1);
|
||||
>foo2 : Symbol(foo2, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 11, 1))
|
||||
>undefined : Symbol(undefined)
|
||||
|
||||
foo3(undefined, 1);
|
||||
>foo3 : Symbol(foo3, Decl(defaultParameterAddsUndefinedWithStrictNullChecks.ts, 15, 1))
|
||||
>undefined : Symbol(undefined)
|
||||
|
||||
|
||||
// .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"
|
||||
|
||||
|
|
|
@ -1,8 +1,120 @@
|
|||
=== tests/cases/compiler/defaultParameterAddsUndefinedWithStrictNullChecks.ts ===
|
||||
function f(addUndefined1 = "J", addUndefined2?: number) {
|
||||
>f : (addUndefined1?: string | undefined, addUndefined2?: number | undefined) => void
|
||||
>f : (addUndefined1?: string | undefined, addUndefined2?: number | undefined) => number
|
||||
>addUndefined1 : string | undefined
|
||||
>"J" : "J"
|
||||
>addUndefined2 : number | undefined
|
||||
|
||||
return addUndefined1.length + (addUndefined2 || 0);
|
||||
>addUndefined1.length + (addUndefined2 || 0) : number
|
||||
>addUndefined1.length : number
|
||||
>addUndefined1 : string
|
||||
>length : number
|
||||
>(addUndefined2 || 0) : number
|
||||
>addUndefined2 || 0 : number
|
||||
>addUndefined2 : number | undefined
|
||||
>0 : 0
|
||||
}
|
||||
function g(addUndefined = "J", addDefined: number) {
|
||||
>g : (addUndefined: string | undefined, addDefined: number) => number
|
||||
>addUndefined : string | undefined
|
||||
>"J" : "J"
|
||||
>addDefined : number
|
||||
|
||||
return addUndefined.length + addDefined;
|
||||
>addUndefined.length + addDefined : number
|
||||
>addUndefined.length : number
|
||||
>addUndefined : string
|
||||
>length : number
|
||||
>addDefined : number
|
||||
}
|
||||
let total = f() + f('a', 1) + f('b') + f(undefined, 2);
|
||||
>total : number
|
||||
>f() + f('a', 1) + f('b') + f(undefined, 2) : number
|
||||
>f() + f('a', 1) + f('b') : number
|
||||
>f() + f('a', 1) : number
|
||||
>f() : number
|
||||
>f : (addUndefined1?: string | undefined, addUndefined2?: number | undefined) => number
|
||||
>f('a', 1) : number
|
||||
>f : (addUndefined1?: string | undefined, addUndefined2?: number | undefined) => number
|
||||
>'a' : "a"
|
||||
>1 : 1
|
||||
>f('b') : number
|
||||
>f : (addUndefined1?: string | undefined, addUndefined2?: number | undefined) => number
|
||||
>'b' : "b"
|
||||
>f(undefined, 2) : number
|
||||
>f : (addUndefined1?: string | undefined, addUndefined2?: number | undefined) => number
|
||||
>undefined : undefined
|
||||
>2 : 2
|
||||
|
||||
total = g('c', 3) + g(undefined, 4);
|
||||
>total = g('c', 3) + g(undefined, 4) : number
|
||||
>total : number
|
||||
>g('c', 3) + g(undefined, 4) : number
|
||||
>g('c', 3) : number
|
||||
>g : (addUndefined: string | undefined, addDefined: number) => number
|
||||
>'c' : "c"
|
||||
>3 : 3
|
||||
>g(undefined, 4) : number
|
||||
>g : (addUndefined: string | undefined, addDefined: number) => number
|
||||
>undefined : undefined
|
||||
>4 : 4
|
||||
|
||||
function foo1(x: string = "string", b: number) {
|
||||
>foo1 : (x: string | undefined, b: number) => void
|
||||
>x : string | undefined
|
||||
>"string" : "string"
|
||||
>b : number
|
||||
|
||||
x.length;
|
||||
>x.length : number
|
||||
>x : string
|
||||
>length : number
|
||||
}
|
||||
|
||||
function foo2(x: string | undefined = "string", b: number) {
|
||||
>foo2 : (x: string | undefined, b: number) => void
|
||||
>x : string | undefined
|
||||
>"string" : "string"
|
||||
>b : number
|
||||
|
||||
x.length; // ok, should be narrowed to string
|
||||
>x.length : number
|
||||
>x : string
|
||||
>length : number
|
||||
}
|
||||
|
||||
function foo3(x = "string", b: number) {
|
||||
>foo3 : (x: string | undefined, b: number) => void
|
||||
>x : string | undefined
|
||||
>"string" : "string"
|
||||
>b : number
|
||||
|
||||
x.length; // ok, should be narrowed to string
|
||||
>x.length : number
|
||||
>x : string
|
||||
>length : number
|
||||
}
|
||||
|
||||
foo1(undefined, 1);
|
||||
>foo1(undefined, 1) : void
|
||||
>foo1 : (x: string | undefined, b: number) => void
|
||||
>undefined : undefined
|
||||
>1 : 1
|
||||
|
||||
foo2(undefined, 1);
|
||||
>foo2(undefined, 1) : void
|
||||
>foo2 : (x: string | undefined, b: number) => void
|
||||
>undefined : undefined
|
||||
>1 : 1
|
||||
|
||||
foo3(undefined, 1);
|
||||
>foo3(undefined, 1) : void
|
||||
>foo3 : (x: string | undefined, b: number) => void
|
||||
>undefined : undefined
|
||||
>1 : 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"
|
||||
|
||||
|
|
|
@ -21,17 +21,10 @@ function foo3(x = "string", b: number) {
|
|||
x.length; // ok, should be narrowed to string
|
||||
}
|
||||
|
||||
function foo4(x: string | undefined, b: number) {
|
||||
x.length; // error, Object is possibly 'undefined'
|
||||
}
|
||||
|
||||
foo1(undefined, 1);
|
||||
foo2(undefined, 1);
|
||||
foo3(undefined, 1);
|
||||
foo4(undefined, 1);
|
||||
|
||||
|
||||
// all four functions should have `x: string| undefined` in their type
|
||||
// .d.ts should have `T | undefined` for all of them
|
||||
// foo2 to have x be initialized on the first line
|
||||
// need to remove special-case code to allow calling foo1(undefined) for x: string = "string"
|
||||
|
|
Loading…
Reference in a new issue