1/* 2 * Copyright (c) 2023-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 Shape {} 17class Circle extends Shape {x: number = 5} 18class Square extends Shape {y: string = "a"} 19 20function createShape(): Shape { 21 return new Circle() 22} 23 24let c1 = <Circle> createShape() 25 26let c2 = createShape() as Circle 27 28// No report is provided during compilation 29// nor during runtime if cast is wrong: 30let c3 = createShape() as Square 31console.log(c3.y) // undefined 32 33// Important corner case for casting primitives to the boxed counterparts: 34// The left operand is not properly boxed here in in runtime 35// because "as" has no runtime effect in TypeScript 36let e1 = (5.0 as Number) instanceof Number // false 37 38// Number object is created and instanceof works as expected: 39let e2 = (new Number(5.0)) instanceof Number // true