• 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/compiler/doesNotNarrowUnionOfConstructorsWithInstanceof.ts ===
20declare function AssertType(value:any, type:string):void;
21class A {
22    length: 1
23    constructor() {
24      this.length = 1
25AssertType(this.length = 1, "int");
26AssertType(this.length, "int");
27AssertType(this, "this");
28AssertType(1, "int");
29    }
30}
31
32class B {
33    length: 2
34    constructor() {
35      this.length = 2
36AssertType(this.length = 2, "int");
37AssertType(this.length, "int");
38AssertType(this, "this");
39AssertType(2, "int");
40    }
41}
42
43function getTypedArray(flag: boolean) {
44AssertType(flag ? new A() : new B(), "union");
45AssertType(flag, "boolean");
46AssertType(new A(), "A");
47AssertType(A, "typeof A");
48AssertType(new B(), "B");
49AssertType(B, "typeof B");
50  return flag ? new A() : new B();
51}
52function getTypedArrayConstructor(flag: boolean) {
53AssertType(flag ? A : B, "union");
54AssertType(flag, "boolean");
55AssertType(A, "typeof A");
56AssertType(B, "typeof B");
57  return flag ? A : B;
58}
59const a = getTypedArray(true);              // A | B
60AssertType(a, "union");
61AssertType(getTypedArray(true), "union");
62AssertType(getTypedArray, "(boolean) => union");
63AssertType(true, "boolean");
64
65const b = getTypedArrayConstructor(false);  // A constructor | B constructor
66AssertType(b, "union");
67AssertType(getTypedArrayConstructor(false), "union");
68AssertType(getTypedArrayConstructor, "(boolean) => union");
69AssertType(false, "boolean");
70
71if (!(a instanceof b)) {
72  console.log(a.length);  // Used to be property 'length' does not exist on type 'never'.
73AssertType(console.log(a.length), "void");
74AssertType(console.log, "(...any[]) => void");
75AssertType(a.length, "union");
76}
77
78
79