• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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
16interface Z {
17    foo: number;
18}
19
20
21 // X implements interface Z, which makes relation between X and Y explicit.
22class C implements Z {
23     public foo: number
24     public bar: string;
25
26     constructor() {
27        this.foo = 0
28        this.bar = "Class C";
29     }
30 }
31
32 // Y implements interface Z, which makes relation between X and Y explicit.
33 class C2 implements Z {
34     public foo: number;
35     public bar: boolean
36
37     constructor() {
38        this.foo = 0;
39        this.bar = true;
40     }
41 }
42
43 let x1: Z = new C()
44 let y1: Z = new C2()
45 let x2 = new C()
46 let y2 = new C2()
47
48console.log("Assign X to Y")
49y2 = x2 // ok, both are of the same type
50
51console.log("Assign X to Y")
52y1 = x1 // ok, both are of the same type
53
54console.log("Assign Y to X")
55x1 = y1 // ok, both are of the same type
56