• 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/members/typesWithSpecializedCallSignatures.ts ===
20declare function AssertType(value:any, type:string):void;
21// basic uses of specialized signatures without errors
22
23class Base { foo: string }
24class Derived1 extends Base { bar: string }
25class Derived2 extends Base { baz: string }
26
27class C {
28    foo(x: 'hi'): Derived1;
29    foo(x: 'bye'): Derived2;
30    foo(x: string): Base;
31    foo(x) {
32AssertType(x, "any");
33        return x;
34    }
35}
36let c = new C();
37AssertType(c, "C");
38AssertType(new C(), "C");
39AssertType(C, "typeof C");
40
41interface I {
42    foo(x: 'hi'): Derived1;
43    foo(x: 'bye'): Derived2;
44    foo(x: string): Base;
45}
46let i: I;
47AssertType(i, "I");
48
49let a: {
50AssertType(a, "{ foo('hi'): Derived1; foo('bye'): Derived2; foo(string): Base; }");
51
52    foo(x: 'hi'): Derived1;
53AssertType(foo, "{ ('hi'): Derived1; ("bye"): Derived2; (string): Base; }");
54AssertType(x, "string");
55
56    foo(x: 'bye'): Derived2;
57AssertType(foo, "{ ("hi"): Derived1; ('bye'): Derived2; (string): Base; }");
58AssertType(x, "string");
59
60    foo(x: string): Base;
61AssertType(foo, "{ ("hi"): Derived1; ("bye"): Derived2; (string): Base; }");
62AssertType(x, "string");
63
64};
65
66c = i;
67AssertType(c = i, "I");
68AssertType(c, "C");
69AssertType(i, "I");
70
71c = a;
72AssertType(c = a, "{ foo("hi"): Derived1; foo("bye"): Derived2; foo(string): Base; }");
73AssertType(c, "C");
74AssertType(a, "{ foo("hi"): Derived1; foo("bye"): Derived2; foo(string): Base; }");
75
76i = c;
77AssertType(i = c, "C");
78AssertType(i, "I");
79AssertType(c, "C");
80
81i = a;
82AssertType(i = a, "{ foo("hi"): Derived1; foo("bye"): Derived2; foo(string): Base; }");
83AssertType(i, "I");
84AssertType(a, "{ foo("hi"): Derived1; foo("bye"): Derived2; foo(string): Base; }");
85
86a = c;
87AssertType(a = c, "C");
88AssertType(a, "{ foo("hi"): Derived1; foo("bye"): Derived2; foo(string): Base; }");
89AssertType(c, "C");
90
91a = i;
92AssertType(a = i, "I");
93AssertType(a, "{ foo("hi"): Derived1; foo("bye"): Derived2; foo(string): Base; }");
94AssertType(i, "I");
95
96let r1: Derived1 = c.foo('hi');
97AssertType(r1, "Derived1");
98AssertType(c.foo('hi'), "Derived1");
99AssertType(c.foo, "{ ("hi"): Derived1; ("bye"): Derived2; (string): Base; }");
100AssertType('hi', "string");
101
102let r2: Derived2 = c.foo('bye');
103AssertType(r2, "Derived2");
104AssertType(c.foo('bye'), "Derived2");
105AssertType(c.foo, "{ ("hi"): Derived1; ("bye"): Derived2; (string): Base; }");
106AssertType('bye', "string");
107
108let r3: Base = c.foo('hm');
109AssertType(r3, "Base");
110AssertType(c.foo('hm'), "Base");
111AssertType(c.foo, "{ ("hi"): Derived1; ("bye"): Derived2; (string): Base; }");
112AssertType('hm', "string");
113
114
115