1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
=== tests/cases/compiler/defaultArgsInFunctionExpressions.ts ===
var f = function (a = 3) { return a; }; // Type should be (a?: number) => number
>f : (a?: number) => number
>function (a = 3) { return a; } : (a?: number) => number
>a : number
>3 : 3
>a : number
var n: number = f(4);
>n : number
>f(4) : number
>f : (a?: number) => number
>4 : 4
n = f();
>n = f() : number
>n : number
>f() : number
>f : (a?: number) => number
var s: string = f('');
>s : string
>f('') : number
>f : (a?: number) => number
>'' : ""
s = f();
>s = f() : number
>s : string
>f() : number
>f : (a?: number) => number
// Type check the default argument with the type annotation
var f2 = function (a: string = 3) { return a; }; // Should error, but be of type (a: string) => string;
>f2 : (a?: string) => string
>function (a: string = 3) { return a; } : (a?: string) => string
>a : string
>3 : 3
>a : string
s = f2('');
>s = f2('') : string
>s : string
>f2('') : string
>f2 : (a?: string) => string
>'' : ""
s = f2();
>s = f2() : string
>s : string
>f2() : string
>f2 : (a?: string) => string
n = f2();
>n = f2() : string
>n : number
>f2() : string
>f2 : (a?: string) => string
// Contextually type the default arg with the type annotation
var f3 = function (a: (s: string) => any = (s) => <number>s) { };
>f3 : (a?: (s: string) => any) => void
>function (a: (s: string) => any = (s) => <number>s) { } : (a?: (s: string) => any) => void
>a : (s: string) => any
>s : string
>(s) => <number>s : (s: string) => number
>s : string
><number>s : number
>s : string
// Type check using the function's contextual type
var f4: (a: number) => void = function (a = "") { };
>f4 : (a: number) => void
>a : number
>function (a = "") { } : (a?: number) => void
>a : number
>"" : ""
// Contextually type the default arg using the function's contextual type
var f5: (a: (s: string) => any) => void = function (a = s => <number>s) { };
>f5 : (a: (s: string) => any) => void
>a : (s: string) => any
>s : string
>function (a = s => <number>s) { } : (a?: (s: string) => any) => void
>a : (s: string) => any
>s => <number>s : (s: string) => number
>s : string
><number>s : number
>s : string
// Instantiated module
module T { }
module U {
>U : typeof U
export var x;
>x : any
}
var f6 = (t = T) => { };
>f6 : (t?: any) => void
>(t = T) => { } : (t?: any) => void
>t : any
>T : any
var f7 = (t = U) => { return t; };
>f7 : (t?: typeof U) => typeof U
>(t = U) => { return t; } : (t?: typeof U) => typeof U
>t : typeof U
>U : typeof U
>t : typeof U
f7().x;
>f7().x : any
>f7() : typeof U
>f7 : (t?: typeof U) => typeof U
>x : any
|