1/* 2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16class X {} 17 18const a = new X() instanceof Object; // true 19const b = new X() instanceof X; // true 20 21// left operand is a type: 22const c = X instanceof Object; // Compile-time error 23const d = X instanceof X; // Compile-time error 24 25// left operand may be of any reference type, like number 26const e = (5.0 as Number) instanceof Number; // false 27 28const g = new Number(3) instanceof Number; 29 30let bad2Number = 5.0 as Number; 31let pi = 3.14; 32bad2Number = pi as Number; 33 34let bad2Boolean = true as Boolean; 35bad2Boolean = a as Boolean; 36 37const aa = this instanceof Object; 38const bb = this instanceof X; 39 40class A extends Error { 41 constructor(message?: string) { 42 super(message) 43 console.log(this instanceof Error) 44 } 45} 46 47let ce = new A() 48 49// left operand is a type: 50const fc = 3 instanceof Object; // Compile-time error 51const fd = "s" instanceof Object; // Compile-time error 52 53const ff = new String("d") instanceof Number; 54 55var le = () => { return String instanceof Object }; 56 57class SomeClass { 58 static readonly field = SomeClass instanceof Object; 59 60 methodRet() { 61 return SomeClass instanceof Object; 62 } 63 64 methodTestCondition(): boolean { 65 if (SomeClass instanceof SomeClass) return true; 66 return false; 67 } 68 69 methodWhileCondition() { 70 while (SomeClass instanceof SomeClass) { 71 } 72 } 73 74 methodDoWhileNegativeCondition() { 75 do { 76 } while (!SomeClass instanceof SomeClass) 77 } 78} 79