File: doesNotNarrowUnionOfConstructorsWithInstanceof.types

package info (click to toggle)
node-typescript 5.0.4%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 459,140 kB
  • sloc: javascript: 1,972,754; makefile: 6; sh: 1
file content (84 lines) | stat: -rw-r--r-- 1,768 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
=== tests/cases/compiler/doesNotNarrowUnionOfConstructorsWithInstanceof.ts ===
class A {
>A : A

    length: 1
>length : 1

    constructor() {
      this.length = 1
>this.length = 1 : 1
>this.length : 1
>this : this
>length : 1
>1 : 1
    }
}

class B {
>B : B

    length: 2
>length : 2

    constructor() {
      this.length = 2
>this.length = 2 : 2
>this.length : 2
>this : this
>length : 2
>2 : 2
    }
}

function getTypedArray(flag: boolean) {
>getTypedArray : (flag: boolean) => A | B
>flag : boolean

  return flag ? new A() : new B();
>flag ? new A() : new B() : A | B
>flag : boolean
>new A() : A
>A : typeof A
>new B() : B
>B : typeof B
}
function getTypedArrayConstructor(flag: boolean) {
>getTypedArrayConstructor : (flag: boolean) => typeof A | typeof B
>flag : boolean

  return flag ? A : B;
>flag ? A : B : typeof A | typeof B
>flag : boolean
>A : typeof A
>B : typeof B
}
const a = getTypedArray(true);              // A | B
>a : A | B
>getTypedArray(true) : A | B
>getTypedArray : (flag: boolean) => A | B
>true : true

const b = getTypedArrayConstructor(false);  // A constructor | B constructor
>b : typeof A | typeof B
>getTypedArrayConstructor(false) : typeof A | typeof B
>getTypedArrayConstructor : (flag: boolean) => typeof A | typeof B
>false : false

if (!(a instanceof b)) {
>!(a instanceof b) : boolean
>(a instanceof b) : boolean
>a instanceof b : boolean
>a : A | B
>b : typeof A | typeof B

  console.log(a.length);  // Used to be property 'length' does not exist on type 'never'.
>console.log(a.length) : void
>console.log : (...data: any[]) => void
>console : Console
>log : (...data: any[]) => void
>a.length : 1 | 2
>a : A | B
>length : 1 | 2
}