• 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/recursiveTypes/recursiveTypesUsedAsFunctionParameters.ts ===
20declare function AssertType(value:any, type:string):void;
21class List<T> {
22    data: T;
23    next: List<List<T>>;
24}
25
26class MyList<T> {
27    data: T;
28    next: MyList<MyList<T>>;
29}
30
31function foo<T>(x: List<T>);
32function foo<U>(x: List<U>); // error, duplicate
33function foo<T>(x: List<T>) {
34}
35
36function foo2<T>(x: List<T>);
37function foo2<U>(x: MyList<U>); // ok, nominally compared with first overload
38function foo2<T>(x: any) {
39}
40
41function other<T extends List<U>, U>() {
42    // error but wrong error
43    // BUG 838247
44    function foo3<V>(x: T);
45AssertType(foo3, "<V>(T) => any");
46AssertType(x, "T");
47
48    function foo3<V>(x: MyList<V>) {
49AssertType(foo3, "<V>(T) => any");
50
51AssertType(x, "MyList<V>");
52}
53
54    // should be error
55    // BUG 838247
56    function foo4<V>(x: T);
57AssertType(foo4, "<V>(T) => any");
58AssertType(x, "T");
59
60    function foo4<V>(x: List<V>) {
61AssertType(foo4, "<V>(T) => any");
62
63AssertType(x, "List<V>");
64}
65
66    // ok
67    function foo5<V>(x: T): string;
68AssertType(foo5, "{ <V>(T): string; <V>(List<V>): number; <V>(MyList<V>): boolean; }");
69AssertType(x, "T");
70
71    function foo5<V>(x: List<V>): number;
72AssertType(foo5, "{ <V>(T): string; <V>(List<V>): number; <V>(MyList<V>): boolean; }");
73AssertType(x, "List<V>");
74
75    function foo5<V>(x: MyList<V>): boolean;
76AssertType(foo5, "{ <V>(T): string; <V>(List<V>): number; <V>(MyList<V>): boolean; }");
77AssertType(x, "MyList<V>");
78
79    function foo5<V>(x: any): any {
80AssertType(foo5, "{ <V>(T): string; <V>(List<V>): number; <V>(MyList<V>): boolean; }");
81return null;
82
83AssertType(x, "any");
84
85AssertType(null, "null");
86}
87
88    let list: List<string>;
89AssertType(list, "List<string>");
90
91    let myList: MyList<string>;
92AssertType(myList, "MyList<string>");
93
94    let r = foo5(list);
95AssertType(r, "number");
96AssertType(foo5(list), "number");
97AssertType(foo5, "{ <V>(T): string; <V>(List<V>): number; <V>(MyList<V>): boolean; }");
98AssertType(list, "List<string>");
99
100    let r2 = foo5(myList);
101AssertType(r2, "number");
102AssertType(foo5(myList), "number");
103AssertType(foo5, "{ <V>(T): string; <V>(List<V>): number; <V>(MyList<V>): boolean; }");
104AssertType(myList, "MyList<string>");
105}
106
107