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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
//// [typeArgumentInferenceWithConstraints.ts]
// Generic call with no parameters
function noParams<T extends {}>() { }
noParams();
noParams<string>();
noParams<{}>();
// Generic call with parameters but none use type parameter type
function noGenericParams<T extends number>(n: string) { }
noGenericParams(''); // Valid
noGenericParams<number>('');
noGenericParams<{}>(''); // Error
// Generic call with multiple type parameters and only one used in parameter type annotation
function someGenerics1<T, U extends T>(n: T, m: number) { }
someGenerics1(3, 4); // Valid
someGenerics1<string, number>(3, 4); // Error
someGenerics1<number, {}>(3, 4); // Error
someGenerics1<number, number>(3, 4);
// Generic call with argument of function type whose parameter is of type parameter type
function someGenerics2a<T extends string>(n: (x: T) => void) { }
someGenerics2a((n: string) => n);
someGenerics2a<string>((n: string) => n);
someGenerics2a<string>((n) => n.substr(0));
function someGenerics2b<T extends string, U extends number>(n: (x: T, y: U) => void) { }
someGenerics2b((n: string, x: number) => n);
someGenerics2b<string, number>((n: string, t: number) => n);
someGenerics2b<string, number>((n, t) => n.substr(t * t));
// Generic call with argument of function type whose parameter is not of type parameter type but body/return type uses type parameter
function someGenerics3<T extends Window>(producer: () => T) { }
someGenerics3(() => ''); // Error
someGenerics3<Window>(() => undefined);
someGenerics3<number>(() => 3); // Error
// 2 parameter generic call with argument 1 of type parameter type and argument 2 of function type whose parameter is of type parameter type
function someGenerics4<T, U extends number>(n: T, f: (x: U) => void) { }
someGenerics4(4, () => null); // Valid
someGenerics4<string, number>('', () => 3);
someGenerics4<string, number>('', (x: string) => ''); // Error
someGenerics4<string, number>(null, null);
// 2 parameter generic call with argument 2 of type parameter type and argument 1 of function type whose parameter is of type parameter type
function someGenerics5<U extends number, T>(n: T, f: (x: U) => void) { }
someGenerics5(4, () => null); // Valid
someGenerics5<number, string>('', () => 3);
someGenerics5<number, string>('', (x: string) => ''); // Error
someGenerics5<string, number>(null, null); // Error
// Generic call with multiple arguments of function types that each have parameters of the same generic type
function someGenerics6<A extends number>(a: (a: A) => A, b: (b: A) => A, c: (c: A) => A) { }
someGenerics6(n => n, n => n, n => n); // Valid
someGenerics6<number>(n => n, n => n, n => n);
someGenerics6<number>((n: number) => n, (n: string) => n, (n: number) => n); // Error
someGenerics6<number>((n: number) => n, (n: number) => n, (n: number) => n);
// Generic call with multiple arguments of function types that each have parameters of different generic type
function someGenerics7<A, B extends string, C>(a: (a: A) => A, b: (b: B) => B, c: (c: C) => C) { }
someGenerics7(n => n, n => n, n => n); // Valid, types of n are <any, string, any> respectively
someGenerics7<number, string, number>(n => n, n => n, n => n);
someGenerics7<number, string, number>((n: number) => n, (n: string) => n, (n: number) => n);
// Generic call with argument of generic function type
function someGenerics8<T extends string>(n: T): T { return n; }
var x = someGenerics8<string>(someGenerics7); // Error
x<string, string, string>(null, null, null); // Error
// Generic call with multiple parameters of generic type passed arguments with no best common type
function someGenerics9<T extends any>(a: T, b: T, c: T): T {
return null;
}
var a9a = someGenerics9('', 0, []);
var a9a: {};
var a9b = someGenerics9<{ a?: number; b?: string; }>({ a: 0 }, { b: '' }, null);
var a9b: { a?: number; b?: string; };
// Generic call with multiple parameters of generic type passed arguments with multiple best common types
interface A91 {
x: number;
y?: string;
}
interface A92 {
x: number;
z?: Window;
}
var a9e = someGenerics9(undefined, { x: 6, z: window }, { x: 6, y: '' });
var a9e: {};
var a9f = someGenerics9<A92>(undefined, { x: 6, z: window }, { x: 6, y: '' });
var a9f: A92;
// Generic call with multiple parameters of generic type passed arguments with a single best common type
var a9d = someGenerics9({ x: 3 }, { x: 6 }, { x: 6 });
var a9d: { x: number; };
// Generic call with multiple parameters of generic type where one argument is of type 'any'
var anyVar: any;
var a = someGenerics9(7, anyVar, 4);
var a: any;
// Generic call with multiple parameters of generic type where one argument is [] and the other is not 'any'
var arr = someGenerics9([], null, undefined);
var arr: any[];
//// [typeArgumentInferenceWithConstraints.js]
// Generic call with no parameters
function noParams() { }
noParams();
noParams();
noParams();
// Generic call with parameters but none use type parameter type
function noGenericParams(n) { }
noGenericParams(''); // Valid
noGenericParams('');
noGenericParams(''); // Error
// Generic call with multiple type parameters and only one used in parameter type annotation
function someGenerics1(n, m) { }
someGenerics1(3, 4); // Valid
someGenerics1(3, 4); // Error
someGenerics1(3, 4); // Error
someGenerics1(3, 4);
// Generic call with argument of function type whose parameter is of type parameter type
function someGenerics2a(n) { }
someGenerics2a(function (n) { return n; });
someGenerics2a(function (n) { return n; });
someGenerics2a(function (n) { return n.substr(0); });
function someGenerics2b(n) { }
someGenerics2b(function (n, x) { return n; });
someGenerics2b(function (n, t) { return n; });
someGenerics2b(function (n, t) { return n.substr(t * t); });
// Generic call with argument of function type whose parameter is not of type parameter type but body/return type uses type parameter
function someGenerics3(producer) { }
someGenerics3(function () { return ''; }); // Error
someGenerics3(function () { return undefined; });
someGenerics3(function () { return 3; }); // Error
// 2 parameter generic call with argument 1 of type parameter type and argument 2 of function type whose parameter is of type parameter type
function someGenerics4(n, f) { }
someGenerics4(4, function () { return null; }); // Valid
someGenerics4('', function () { return 3; });
someGenerics4('', function (x) { return ''; }); // Error
someGenerics4(null, null);
// 2 parameter generic call with argument 2 of type parameter type and argument 1 of function type whose parameter is of type parameter type
function someGenerics5(n, f) { }
someGenerics5(4, function () { return null; }); // Valid
someGenerics5('', function () { return 3; });
someGenerics5('', function (x) { return ''; }); // Error
someGenerics5(null, null); // Error
// Generic call with multiple arguments of function types that each have parameters of the same generic type
function someGenerics6(a, b, c) { }
someGenerics6(function (n) { return n; }, function (n) { return n; }, function (n) { return n; }); // Valid
someGenerics6(function (n) { return n; }, function (n) { return n; }, function (n) { return n; });
someGenerics6(function (n) { return n; }, function (n) { return n; }, function (n) { return n; }); // Error
someGenerics6(function (n) { return n; }, function (n) { return n; }, function (n) { return n; });
// Generic call with multiple arguments of function types that each have parameters of different generic type
function someGenerics7(a, b, c) { }
someGenerics7(function (n) { return n; }, function (n) { return n; }, function (n) { return n; }); // Valid, types of n are <any, string, any> respectively
someGenerics7(function (n) { return n; }, function (n) { return n; }, function (n) { return n; });
someGenerics7(function (n) { return n; }, function (n) { return n; }, function (n) { return n; });
// Generic call with argument of generic function type
function someGenerics8(n) { return n; }
var x = someGenerics8(someGenerics7); // Error
x(null, null, null); // Error
// Generic call with multiple parameters of generic type passed arguments with no best common type
function someGenerics9(a, b, c) {
return null;
}
var a9a = someGenerics9('', 0, []);
var a9a;
var a9b = someGenerics9({ a: 0 }, { b: '' }, null);
var a9b;
var a9e = someGenerics9(undefined, { x: 6, z: window }, { x: 6, y: '' });
var a9e;
var a9f = someGenerics9(undefined, { x: 6, z: window }, { x: 6, y: '' });
var a9f;
// Generic call with multiple parameters of generic type passed arguments with a single best common type
var a9d = someGenerics9({ x: 3 }, { x: 6 }, { x: 6 });
var a9d;
// Generic call with multiple parameters of generic type where one argument is of type 'any'
var anyVar;
var a = someGenerics9(7, anyVar, 4);
var a;
// Generic call with multiple parameters of generic type where one argument is [] and the other is not 'any'
var arr = someGenerics9([], null, undefined);
var arr;
|