• 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/typeInference/genericCallWithObjectTypeArgs2.ts ===
20declare function AssertType(value:any, type:string):void;
21class Base {
22    x: string;
23}
24class Derived extends Base {
25    y: string;
26}
27class Derived2 extends Base {
28    z: string;
29}
30
31// returns {}[]
32function f<T extends Base, U extends Base>(a: { x: T; y: U }) {
33AssertType([a.x, a.y], "(union)[]");
34AssertType(a.x, "T");
35AssertType(a.y, "U");
36    return [a.x, a.y];
37}
38
39let r = f({ x: new Derived(), y: new Derived2() }); // {}[]
40AssertType(r, "(union)[]");
41AssertType(f({ x: new Derived(), y: new Derived2() }), "(union)[]");
42AssertType(f, "<T extends Base, U extends Base>({ x: T; y: U; }) => (union)[]");
43AssertType({ x: new Derived(), y: new Derived2() }, "{ x: Derived; y: Derived2; }");
44AssertType(x, "Derived");
45AssertType(new Derived(), "Derived");
46AssertType(Derived, "typeof Derived");
47AssertType(y, "Derived2");
48AssertType(new Derived2(), "Derived2");
49AssertType(Derived2, "typeof Derived2");
50
51let r2 = f({ x: new Base(), y: new Derived2() }); // {}[]
52AssertType(r2, "(union)[]");
53AssertType(f({ x: new Base(), y: new Derived2() }), "(union)[]");
54AssertType(f, "<T extends Base, U extends Base>({ x: T; y: U; }) => (union)[]");
55AssertType({ x: new Base(), y: new Derived2() }, "{ x: Base; y: Derived2; }");
56AssertType(x, "Base");
57AssertType(new Base(), "Base");
58AssertType(Base, "typeof Base");
59AssertType(y, "Derived2");
60AssertType(new Derived2(), "Derived2");
61AssertType(Derived2, "typeof Derived2");
62
63
64function f2<T extends Base, U extends Base>(a: { x: T; y: U }) {
65AssertType((x: T) => a.y, "(T) => U");
66AssertType(x, "T");
67AssertType(a.y, "U");
68    return (x: T) => a.y;
69}
70
71let r3 = f2({ x: new Derived(), y: new Derived2() }); // Derived => Derived2
72AssertType(r3, "(Derived) => Derived2");
73AssertType(f2({ x: new Derived(), y: new Derived2() }), "(Derived) => Derived2");
74AssertType(f2, "<T extends Base, U extends Base>({ x: T; y: U; }) => (T) => U");
75AssertType({ x: new Derived(), y: new Derived2() }, "{ x: Derived; y: Derived2; }");
76AssertType(x, "Derived");
77AssertType(new Derived(), "Derived");
78AssertType(Derived, "typeof Derived");
79AssertType(y, "Derived2");
80AssertType(new Derived2(), "Derived2");
81AssertType(Derived2, "typeof Derived2");
82
83interface I<T, U> {
84    x: T;
85    y: U;
86}
87
88let i: I<Base, Derived>;
89AssertType(i, "I<Base, Derived>");
90
91let r4 = f2(i); // Base => Derived
92AssertType(r4, "(Base) => Derived");
93AssertType(f2(i), "(Base) => Derived");
94AssertType(f2, "<T extends Base, U extends Base>({ x: T; y: U; }) => (T) => U");
95AssertType(i, "I<Base, Derived>");
96
97
98