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 16let o1 = {n: 42, s: "foo"} 17let o2: Object = {n: 42, s: "foo"} 18let o3: object = {n: 42, s: "foo"} 19 20let oo: Object[] = [{n: 1, s: "1"}, {n: 2, s: "2"}] 21 22class C2 { 23 s: string 24 constructor(s: string) { 25 this.s = "s =" + s 26 } 27} 28let o4: C2 = {s: "foo"} 29 30class C3 { 31 readonly n: number = 0 32 readonly s: string = "" 33} 34let o5: C3 = {n: 42, s: "foo"} 35 36abstract class A {} 37let o6: A = {} 38 39class C4 { 40 n: number = 0 41 s: string = "" 42 f() { 43 console.log("Hello") 44 } 45} 46let o7: C4 = {n: 42, s: "foo", f : () => {}} 47 48class Point { 49 x: number = 0 50 y: number = 0 51} 52function id_x_y(o: Point): Point { 53 return o 54} 55 56// Structural typing is used to deduce that p is Point: 57let p = {x: 5, y: 10} 58id_x_y(p) 59 60// A literal can be contextually (i.e., implicitly) typed as Point: 61id_x_y({x: 5, y: 10})