• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/typeRelationships/subtypesAndSuperTypes/subtypingWithCallSignatures3.ts ===
20declare function AssertType(value:any, type:string):void;
21
22// checking subtype relations for function types as it relates to contextual signature instantiation
23// error cases, so function calls will all result in 'any'
24
25module Errors {
26    class Base { foo: string; }
27    class Derived extends Base { bar: string; }
28    class Derived2 extends Derived { baz: string; }
29    class OtherDerived extends Base { bing: string; }
30
31    declare function foo2(a2: (x: number) => string[]): typeof a2;
32    declare function foo2(a2: any): any;
33
34    declare function foo7(a2: (x: (arg: Base) => Derived) => (r: Base) => Derived2): typeof a2;
35    declare function foo7(a2: any): any;
36
37    declare function foo8(a2: (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived): typeof a2;
38    declare function foo8(a2: any): any;
39
40    declare function foo10(a2: (...x: Base[]) => Base): typeof a2;
41    declare function foo10(a2: any): any;
42
43    declare function foo11(a2: (x: { foo: string }, y: { foo: string; bar: string }) => Base): typeof a2;
44    declare function foo11(a2: any): any;
45
46    declare function foo12(a2: (x: Array<Base>, y: Array<Derived2>) => Array<Derived>): typeof a2;
47    declare function foo12(a2: any): any;
48
49    declare function foo15(a2: (x: { a: string; b: number }) => number): typeof a2;
50    declare function foo15(a2: any): any;
51
52    declare function foo16(a2: {
53        // type of parameter is overload set which means we can't do inference based on this type
54        (x: {
55            (a: number): number;
56            (a?: number): number;
57        }): number[];
58        (x: {
59            (a: boolean): boolean;
60            (a?: boolean): boolean;
61        }): boolean[];
62    }): typeof a2;
63    declare function foo16(a2: any): any;
64
65    declare function foo17(a2: {
66        (x: {
67            <T extends Derived>(a: T): T;
68            <T extends Base>(a: T): T;
69        }): any[];
70        (x: {
71            <T extends Derived2>(a: T): T;
72            <T extends Base>(a: T): T;
73        }): any[];
74    }): typeof a2;
75    declare function foo17(a2: any): any;
76
77    let r1 = foo2(<T, U>(x: T) => <U[]>null); // any
78    let r1a = [(x: number) => [''], <T, U>(x: T) => <U[]>null];
79    let r1b = [<T, U>(x: T) => <U[]>null, (x: number) => ['']];
80
81    let r2arg = <T extends Base, U extends Derived, V extends Derived2>(x: (arg: T) => U) => (r: T) => <V>null;
82    let r2arg2 = (x: (arg: Base) => Derived) => (r: Base) => <Derived2>null;
83    let r2 = foo7(r2arg); // any
84    let r2a = [r2arg2, r2arg];
85    let r2b = [r2arg, r2arg2];
86
87    let r3arg = <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => <U>null;
88    let r3arg2 = (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => <Derived>null;
89    let r3 = foo8(r3arg); // any
90    let r3a = [r3arg2, r3arg];
91    let r3b = [r3arg, r3arg2];
92
93    let r4arg = <T extends Derived>(...x: T[]) => <T>null;
94    let r4arg2 = (...x: Base[]) => <Base>null;
95    let r4 = foo10(r4arg); // any
96    let r4a = [r4arg2, r4arg];
97    let r4b = [r4arg, r4arg2];
98
99    let r5arg = <T extends Derived>(x: T, y: T) => <T>null;
100    let r5arg2 = (x: { foo: string }, y: { foo: string; bar: string }) => <Base>null;
101    let r5 = foo11(r5arg); // any
102    let r5a = [r5arg2, r5arg];
103    let r5b = [r5arg, r5arg2];
104
105    let r6arg = (x: Array<Base>, y: Array<Derived2>) => <Array<Derived>>null;
106    let r6arg2 = <T extends Array<Derived2>>(x: Array<Base>, y: Array<Base>) => <T>null;
107    let r6 = foo12(r6arg); // (x: Array<Base>, y: Array<Derived2>) => Array<Derived>
108    let r6a = [r6arg2, r6arg];
109    let r6b = [r6arg, r6arg2];
110
111    let r7arg = <T>(x: { a: T; b: T }) => <T>null;
112    let r7arg2 = (x: { a: string; b: number }) => 1;
113    let r7 = foo15(r7arg); // any
114    let r7a = [r7arg2, r7arg];
115    let r7b = [r7arg, r7arg2];
116
117    let r7arg3 = <T extends Base>(x: { a: T; b: T }) => 1;
118    let r7c = foo15(r7arg3); // (x: { a: string; b: number }) => number): number;
119    let r7d = [r7arg2, r7arg3];
120    let r7e = [r7arg3, r7arg2];
121
122    let r8arg = <T>(x: (a: T) => T) => <T[]>null;
123    let r8 = foo16(r8arg); // any
124
125    let r9arg = <T>(x: (a: T) => T) => <any[]>null;
126    let r9 = foo17(r9arg); // (x: { <T extends Derived >(a: T): T; <T extends Base >(a: T): T; }): any[]; (x: { <T extends Derived2>(a: T): T; <T extends Base>(a: T): T; }): any[];
127}
128
129module WithGenericSignaturesInBaseType {
130    declare function foo2(a2: <T>(x: T) => T[]): typeof a2;
131    declare function foo2(a2: any): any;
132    let r2arg2 = <T>(x: T) => [''];
133    let r2 = foo2(r2arg2); // <T>(x:T) => T[] since we can infer from generic signatures now
134
135    declare function foo3(a2: <T>(x: T) => string[]): typeof a2;
136    declare function foo3(a2: any): any;
137    let r3arg2 = <T>(x: T) => <T[]>null;
138    let r3 = foo3(r3arg2); // any
139}
140
141