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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
=== tests/cases/compiler/instanceOfAssignability.ts ===
interface Base {
foo: string|number;
>foo : string | number
optional?: number;
>optional : number
}
// Derived1 is assignable to, but not a subtype of, Base
class Derived1 implements Base {
>Derived1 : Derived1
foo: string;
>foo : string
}
// Derived2 is a subtype of Base that is not assignable to Derived1
class Derived2 implements Base {
>Derived2 : Derived2
foo: number;
>foo : number
optional: number;
>optional : number
}
class Animal {
>Animal : Animal
move;
>move : any
}
class Mammal extends Animal { milk; }
>Mammal : Mammal
>Animal : Animal
>milk : any
class Giraffe extends Mammal { neck; }
>Giraffe : Giraffe
>Mammal : Mammal
>neck : any
function fn1(x: Array<number>|Array<string>|boolean) {
>fn1 : (x: boolean | number[] | string[]) => void
>x : boolean | number[] | string[]
if(x instanceof Array) {
>x instanceof Array : boolean
>x : boolean | number[] | string[]
>Array : ArrayConstructor
// 1.5: y: Array<number>|Array<string>
// Want: y: Array<number>|Array<string>
let y = x;
>y : number[] | string[]
>x : number[] | string[]
}
}
function fn2(x: Base) {
>fn2 : (x: Base) => void
>x : Base
if(x instanceof Derived1) {
>x instanceof Derived1 : boolean
>x : Base
>Derived1 : typeof Derived1
// 1.5: y: Base
// Want: y: Derived1
let y = x;
>y : Derived1
>x : Derived1
}
}
function fn3(x: Base|Derived1) {
>fn3 : (x: Base | Derived1) => void
>x : Base | Derived1
if(x instanceof Derived2) {
>x instanceof Derived2 : boolean
>x : Base | Derived1
>Derived2 : typeof Derived2
// 1.5: y: Derived2
// Want: Derived2
let y = x;
>y : Derived2
>x : Derived2
}
}
function fn4(x: Base|Derived2) {
>fn4 : (x: Base | Derived2) => void
>x : Base | Derived2
if(x instanceof Derived1) {
>x instanceof Derived1 : boolean
>x : Base | Derived2
>Derived1 : typeof Derived1
// 1.5: y: {}
// Want: Derived1
let y = x;
>y : Derived1
>x : Derived1
}
}
function fn5(x: Derived1) {
>fn5 : (x: Derived1) => void
>x : Derived1
if(x instanceof Derived2) {
>x instanceof Derived2 : boolean
>x : Derived1
>Derived2 : typeof Derived2
// 1.5: y: Derived1
// Want: ???
let y = x;
>y : Derived1 & Derived2
>x : Derived1 & Derived2
}
}
function fn6(x: Animal|Mammal) {
>fn6 : (x: Animal | Mammal) => void
>x : Animal | Mammal
if(x instanceof Giraffe) {
>x instanceof Giraffe : boolean
>x : Animal | Mammal
>Giraffe : typeof Giraffe
// 1.5: y: Derived1
// Want: ???
let y = x;
>y : Giraffe
>x : Giraffe
}
}
function fn7(x: Array<number>|Array<string>) {
>fn7 : (x: number[] | string[]) => void
>x : number[] | string[]
if(x instanceof Array) {
>x instanceof Array : boolean
>x : number[] | string[]
>Array : ArrayConstructor
// 1.5: y: Array<number>|Array<string>
// Want: y: Array<number>|Array<string>
let y = x;
>y : number[] | string[]
>x : number[] | string[]
}
}
interface Alpha { a }
>a : any
interface Beta { b }
>b : any
interface Gamma { c }
>c : any
class ABC { a; b; c; }
>ABC : ABC
>a : any
>b : any
>c : any
function fn8(x: Alpha|Beta|Gamma) {
>fn8 : (x: Alpha | Beta | Gamma) => void
>x : Alpha | Beta | Gamma
if(x instanceof ABC) {
>x instanceof ABC : boolean
>x : Alpha | Beta | Gamma
>ABC : typeof ABC
let y = x;
>y : ABC
>x : ABC
}
}
|