1/* 2 * Copyright (c) 2024-2025 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 16function is_null(v: Object | null | undefined) { 17 return v instanceof null; 18} 19 20function is_obj(v: Object | null | undefined) { 21 return v instanceof Object; 22} 23 24class Foo { }; 25 26type nullish_obj = Object | null | undefined; 27type nullish_int = Int | null | undefined; 28type nullish_foo = Foo | null | undefined; 29 30function is_nullish_obj(v: nullish_obj) { 31 return v instanceof nullish_obj; 32} 33 34function is_nullish_int(v: nullish_int) { 35 return v instanceof nullish_int; 36} 37 38function is_nullish_foo(v: nullish_foo) { 39 return v instanceof nullish_foo; 40} 41 42function is_nullish_foo_erased(v: nullish_obj) { 43 return v instanceof nullish_foo; 44} 45 46function main() { 47 const obj = new Object(); 48 const foo = new Foo(); 49 const boxint = new Int(3); 50 51 assertEQ(is_null(null), true) 52 assertEQ(is_null(undefined), false) 53 assertEQ(is_null(obj), false) 54 55 assertEQ(is_obj(null), false) 56 assertEQ(is_obj(undefined), false) 57 assertEQ(is_obj(obj), true) 58 59 assertEQ(is_nullish_obj(null), true) 60 assertEQ(is_nullish_obj(undefined), true) 61 assertEQ(is_nullish_obj(obj), true) 62 assertEQ(is_nullish_obj(foo), true) 63 64 assertEQ(is_nullish_int(null), true) 65 assertEQ(is_nullish_int(undefined), true) 66 assertEQ(is_nullish_int(boxint), true) 67 68 assertEQ(is_nullish_foo(null), true) 69 assertEQ(is_nullish_foo(undefined), true) 70 assertEQ(is_nullish_foo(foo), true) 71 72 assertEQ(is_nullish_foo_erased(null), true) 73 assertEQ(is_nullish_foo_erased(undefined), true) 74 assertEQ(is_nullish_foo_erased(obj), false) 75 assertEQ(is_nullish_foo_erased(boxint), false) 76 assertEQ(is_nullish_foo_erased(foo), true) 77} 78