1/* 2* Copyright (c) Microsoft Corporation. All rights reserved. 3* Copyright (c) 2023 Huawei Device Co., Ltd. 4* Licensed under the Apache License, Version 2.0 (the "License"); 5* you may not use this file except in compliance with the License. 6* You may obtain a copy of the License at 7* 8* http://www.apache.org/licenses/LICENSE-2.0 9* 10* Unless required by applicable law or agreed to in writing, software 11* distributed under the License is distributed on an "AS IS" BASIS, 12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13* See the License for the specific language governing permissions and 14* limitations under the License. 15* 16* This file has been modified by Huawei to verify type inference by adding verification statements. 17*/ 18 19// === tests/cases/conformance/types/typeParameters/typeArgumentLists/functionConstraintSatisfaction.ts === 20declare function AssertType(value:any, type:string):void; 21// satisfaction of a constraint to Function, no errors expected 22 23function foo<T extends Function>(x: T): T { 24AssertType(x, "T"); 25return x; 26} 27 28interface I { 29 (): string; 30} 31let i: I; 32AssertType(i, "I"); 33 34class C { 35 foo: string; 36} 37 38let a: { (): string }; 39AssertType(a, "() => string"); 40 41let b: { new (): string }; 42AssertType(b, "new () => string"); 43 44let c: { (): string; (x): string }; 45AssertType(c, "{ (): string; (any): string; }"); 46AssertType(x, "any"); 47 48let r = foo(new Function()); 49AssertType(r, "Function"); 50AssertType(foo(new Function()), "Function"); 51AssertType(foo, "<T extends Function>(T) => T"); 52AssertType(new Function(), "Function"); 53AssertType(Function, "FunctionConstructor"); 54 55let r1 = foo((x) => x); 56AssertType(r1, "(any) => any"); 57AssertType(foo((x) => x), "(any) => any"); 58AssertType(foo, "<T extends Function>(T) => T"); 59AssertType((x) => x, "(any) => any"); 60AssertType(x, "any"); 61AssertType(x, "any"); 62 63let r2 = foo((x: string[]) => x); 64AssertType(r2, "(string[]) => string[]"); 65AssertType(foo((x: string[]) => x), "(string[]) => string[]"); 66AssertType(foo, "<T extends Function>(T) => T"); 67AssertType((x: string[]) => x, "(string[]) => string[]"); 68AssertType(x, "string[]"); 69AssertType(x, "string[]"); 70 71let r3 = foo(function (x) { 72AssertType(r3, "(any) => any"); 73AssertType(foo(function (x) { return x }), "(any) => any"); 74AssertType(foo, "<T extends Function>(T) => T"); 75AssertType(function (x) { return x }, "(any) => any"); 76AssertType(x, "any"); 77AssertType(x, "any"); 78return x }); 79 80let r4 = foo(function (x: string[]) { 81AssertType(r4, "(string[]) => string[]"); 82AssertType(foo(function (x: string[]) { return x }), "(string[]) => string[]"); 83AssertType(foo, "<T extends Function>(T) => T"); 84AssertType(function (x: string[]) { return x }, "(string[]) => string[]"); 85AssertType(x, "string[]"); 86AssertType(x, "string[]"); 87return x }); 88 89let r5 = foo(i); 90AssertType(r5, "I"); 91AssertType(foo(i), "I"); 92AssertType(foo, "<T extends Function>(T) => T"); 93AssertType(i, "I"); 94 95let r6 = foo(C); 96AssertType(r6, "typeof C"); 97AssertType(foo(C), "typeof C"); 98AssertType(foo, "<T extends Function>(T) => T"); 99AssertType(C, "typeof C"); 100 101let r7 = foo(b); 102AssertType(r7, "new () => string"); 103AssertType(foo(b), "new () => string"); 104AssertType(foo, "<T extends Function>(T) => T"); 105AssertType(b, "new () => string"); 106 107let r8 = foo(c); 108AssertType(r8, "{ (): string; (any): string; }"); 109AssertType(foo(c), "{ (): string; (any): string; }"); 110AssertType(foo, "<T extends Function>(T) => T"); 111AssertType(c, "{ (): string; (any): string; }"); 112 113interface I2<T> { 114 (x: T): T; 115} 116let i2: I2<string>; 117AssertType(i2, "I2<string>"); 118 119class C2<T> { 120 foo: T; 121} 122 123let a2: { <T>(x: T): T }; 124AssertType(a2, "<T>(T) => T"); 125AssertType(x, "T"); 126 127let b2: { new <T>(x: T): T }; 128AssertType(b2, "new <T>(T) => T"); 129AssertType(x, "T"); 130 131let c2: { <T>(x: T): T; <T>(x: T, y: T): T }; 132AssertType(c2, "{ <T>(T): T; <T>(T, T): T; }"); 133AssertType(x, "T"); 134AssertType(x, "T"); 135AssertType(y, "T"); 136 137let r9 = foo(<U>(x: U) => x); 138AssertType(r9, "<U>(U) => U"); 139AssertType(foo(<U>(x: U) => x), "<U>(U) => U"); 140AssertType(foo, "<T extends Function>(T) => T"); 141AssertType(<U>(x: U) => x, "<U>(U) => U"); 142AssertType(x, "U"); 143AssertType(x, "U"); 144 145let r10 = foo(function <U>(x: U) { 146AssertType(r10, "<U>(U) => U"); 147AssertType(foo(function <U>(x: U) { return x; }), "<U>(U) => U"); 148AssertType(foo, "<T extends Function>(T) => T"); 149AssertType(function <U>(x: U) { return x; }, "<U>(U) => U"); 150AssertType(x, "U"); 151AssertType(x, "U"); 152return x; }); 153 154let r11 = foo(<U extends Date>(x: U) => x); 155AssertType(r11, "<U extends Date>(U) => U"); 156AssertType(foo(<U extends Date>(x: U) => x), "<U extends Date>(U) => U"); 157AssertType(foo, "<T extends Function>(T) => T"); 158AssertType(<U extends Date>(x: U) => x, "<U extends Date>(U) => U"); 159AssertType(x, "U"); 160AssertType(x, "U"); 161 162let r12 = foo(<U, V>(x: U, y: V) => x); 163AssertType(r12, "<U, V>(U, V) => U"); 164AssertType(foo(<U, V>(x: U, y: V) => x), "<U, V>(U, V) => U"); 165AssertType(foo, "<T extends Function>(T) => T"); 166AssertType(<U, V>(x: U, y: V) => x, "<U, V>(U, V) => U"); 167AssertType(x, "U"); 168AssertType(y, "V"); 169AssertType(x, "U"); 170 171let r13 = foo(i2); 172AssertType(r13, "I2<string>"); 173AssertType(foo(i2), "I2<string>"); 174AssertType(foo, "<T extends Function>(T) => T"); 175AssertType(i2, "I2<string>"); 176 177let r14 = foo(C2); 178AssertType(r14, "typeof C2"); 179AssertType(foo(C2), "typeof C2"); 180AssertType(foo, "<T extends Function>(T) => T"); 181AssertType(C2, "typeof C2"); 182 183let r15 = foo(b2); 184AssertType(r15, "new <T>(T) => T"); 185AssertType(foo(b2), "new <T>(T) => T"); 186AssertType(foo, "<T extends Function>(T) => T"); 187AssertType(b2, "new <T>(T) => T"); 188 189let r16 = foo(c2); 190AssertType(r16, "{ <T>(T): T; <T>(T, T): T; }"); 191AssertType(foo(c2), "{ <T>(T): T; <T>(T, T): T; }"); 192AssertType(foo, "<T extends Function>(T) => T"); 193AssertType(c2, "{ <T>(T): T; <T>(T, T): T; }"); 194 195interface F2 extends Function { foo: string; } 196let f2: F2; 197AssertType(f2, "F2"); 198 199let r17 = foo(f2); 200AssertType(r17, "F2"); 201AssertType(foo(f2), "F2"); 202AssertType(foo, "<T extends Function>(T) => T"); 203AssertType(f2, "F2"); 204 205function foo2<T extends { (): void }, U extends { (): void }>(x: T, y: U) { 206 foo(x); 207AssertType(foo(x), "T"); 208AssertType(foo, "<T extends Function>(T) => T"); 209AssertType(x, "T"); 210 211 foo(y); 212AssertType(foo(y), "U"); 213AssertType(foo, "<T extends Function>(T) => T"); 214AssertType(y, "U"); 215} 216 217