• 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/instanceOf/narrowingConstrainedTypeVariable.ts ===
20declare function AssertType(value:any, type:string):void;
21// Repro from #20138
22
23class C { }
24
25function f1<T extends C>(v: T | string): void {
26    if (v instanceof C) {
27AssertType(v instanceof C, "boolean");
28AssertType(v, "union");
29AssertType(C, "typeof C");
30
31        const x: T = v;
32AssertType(x, "T");
33AssertType(v, "T");
34    }
35    else {
36        const s: string = v;
37AssertType(s, "string");
38AssertType(v, "string");
39    }
40}
41
42class D { }
43
44function f2<T extends C, U extends D>(v: T | U) {
45    if (v instanceof C) {
46AssertType(v instanceof C, "boolean");
47AssertType(v, "union");
48AssertType(C, "typeof C");
49
50        const x: T = v;
51AssertType(x, "T");
52AssertType(v, "T");
53    }
54    else {
55        const y: U = v;
56AssertType(y, "U");
57AssertType(v, "U");
58    }
59}
60
61class E { x: string | undefined }
62
63function f3<T extends E>(v: T | { x: string }) {
64    if (v instanceof E) {
65AssertType(v instanceof E, "boolean");
66AssertType(v, "union");
67AssertType(E, "typeof E");
68
69        const x: T = v;
70AssertType(x, "T");
71AssertType(v, "T");
72    }
73    else {
74        const y: { x: string } = v;
75AssertType(y, "{ x: string; }");
76AssertType(x, "string");
77AssertType(v, "{ x: string; }");
78    }
79}
80
81
82