1/* 2 * Copyright (c) 2023 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/**--- 16 description: > 17 a union type U is a subtype of a type T if each type in U is a subtype of T. 18 a type T is a subtype of a union type U if T is a subtype of any type in U. 19 module: ESNext 20 isCurrent: true 21 ---*/ 22 23 24import { Assert } from '../../../../suite/assert.js' 25 26class ClassT { 27 size: number = 0; 28 description: string = ''; 29} 30class ClassU1 extends ClassT { 31 alive: boolean = false; 32} 33class ClassU2 extends ClassT { 34 weight: number = 0; 35} 36interface InterfaceU1 { 37 speak(): string; 38} 39interface InterfaceU2 { 40 eat(): string; 41} 42class ClassT2 implements InterfaceU1, InterfaceU2 { 43 food: string = ''; 44 language: string = ''; 45 speak() { 46 return this.language; 47 } 48 eat() { 49 return this.food; 50 } 51 constructor(food: string, language: string) { 52 this.food = food; 53 this.language = language; 54 } 55} 56let u1: ClassU1 | ClassU2 = { size: 7, description: "A", alive: false }; 57let t1: ClassT; 58t1 = u1; 59Assert.equal(JSON.stringify(t1), '{"size":7,"description":"A","alive":false}'); 60let u2: InterfaceU1 | InterfaceU2; 61let t2: ClassT2 = new ClassT2("rice", "Chinese"); 62u2 = t2; 63Assert.equal(JSON.stringify(u2), '{"food":"rice","language":"Chinese"}');