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 195
|
//// [instanceOfAssignability.ts]
interface Base {
foo: string|number;
optional?: number;
}
// Derived1 is assignable to, but not a subtype of, Base
class Derived1 implements Base {
foo: string;
}
// Derived2 is a subtype of Base that is not assignable to Derived1
class Derived2 implements Base {
foo: number;
optional: number;
}
class Animal {
move;
}
class Mammal extends Animal { milk; }
class Giraffe extends Mammal { neck; }
function fn1(x: Array<number>|Array<string>|boolean) {
if(x instanceof Array) {
// 1.5: y: Array<number>|Array<string>
// Want: y: Array<number>|Array<string>
let y = x;
}
}
function fn2(x: Base) {
if(x instanceof Derived1) {
// 1.5: y: Base
// Want: y: Derived1
let y = x;
}
}
function fn3(x: Base|Derived1) {
if(x instanceof Derived2) {
// 1.5: y: Derived2
// Want: Derived2
let y = x;
}
}
function fn4(x: Base|Derived2) {
if(x instanceof Derived1) {
// 1.5: y: {}
// Want: Derived1
let y = x;
}
}
function fn5(x: Derived1) {
if(x instanceof Derived2) {
// 1.5: y: Derived1
// Want: ???
let y = x;
}
}
function fn6(x: Animal|Mammal) {
if(x instanceof Giraffe) {
// 1.5: y: Derived1
// Want: ???
let y = x;
}
}
function fn7(x: Array<number>|Array<string>) {
if(x instanceof Array) {
// 1.5: y: Array<number>|Array<string>
// Want: y: Array<number>|Array<string>
let y = x;
}
}
interface Alpha { a }
interface Beta { b }
interface Gamma { c }
class ABC { a; b; c; }
function fn8(x: Alpha|Beta|Gamma) {
if(x instanceof ABC) {
let y = x;
}
}
//// [instanceOfAssignability.js]
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
// Derived1 is assignable to, but not a subtype of, Base
var Derived1 = /** @class */ (function () {
function Derived1() {
}
return Derived1;
}());
// Derived2 is a subtype of Base that is not assignable to Derived1
var Derived2 = /** @class */ (function () {
function Derived2() {
}
return Derived2;
}());
var Animal = /** @class */ (function () {
function Animal() {
}
return Animal;
}());
var Mammal = /** @class */ (function (_super) {
__extends(Mammal, _super);
function Mammal() {
return _super !== null && _super.apply(this, arguments) || this;
}
return Mammal;
}(Animal));
var Giraffe = /** @class */ (function (_super) {
__extends(Giraffe, _super);
function Giraffe() {
return _super !== null && _super.apply(this, arguments) || this;
}
return Giraffe;
}(Mammal));
function fn1(x) {
if (x instanceof Array) {
// 1.5: y: Array<number>|Array<string>
// Want: y: Array<number>|Array<string>
var y = x;
}
}
function fn2(x) {
if (x instanceof Derived1) {
// 1.5: y: Base
// Want: y: Derived1
var y = x;
}
}
function fn3(x) {
if (x instanceof Derived2) {
// 1.5: y: Derived2
// Want: Derived2
var y = x;
}
}
function fn4(x) {
if (x instanceof Derived1) {
// 1.5: y: {}
// Want: Derived1
var y = x;
}
}
function fn5(x) {
if (x instanceof Derived2) {
// 1.5: y: Derived1
// Want: ???
var y = x;
}
}
function fn6(x) {
if (x instanceof Giraffe) {
// 1.5: y: Derived1
// Want: ???
var y = x;
}
}
function fn7(x) {
if (x instanceof Array) {
// 1.5: y: Array<number>|Array<string>
// Want: y: Array<number>|Array<string>
var y = x;
}
}
var ABC = /** @class */ (function () {
function ABC() {
}
return ABC;
}());
function fn8(x) {
if (x instanceof ABC) {
var y = x;
}
}
|