1/* 2 * Copyright (c) 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 16abstract class Y { 17 abstract getDataByName(name: string | number, albumUri: string): Promise<void>; 18} 19 20class X extends Y { 21 async getDataByName(name: string, albumUri: string): Promise<undefined> { // error 2 22 return; 23 } 24} 25 26abstract class B { 27 abstract getDataByName(name: string | number, albumUri: string): Promise<undefined> | Promise<void>; 28} 29 30class A extends B { 31 async getDataByName(name: string | number | boolean, albumUri: string): Promise<undefined> { //Legal 32 return undefined; 33 } 34} 35 36abstract class W { 37 abstract getDataByName(name: string | number, albumUri: string): Promise<void>; 38} 39 40class Q extends W { 41 async getDataByName(name: string | number, albumUri: string | number): Promise<undefined> {// error 1 42 return; 43 }; 44} 45 46abstract class BaseClass3 { 47 abstract compute(value: string): string; 48} 49 50class IncorrectWiderReturn extends BaseClass3 { 51 compute(value: string): string | number {// error 1 52 return value.length > 5 ? value : 0; 53 } 54} 55 56abstract class BaseClass4 { 57 abstract setValues(x: string | number, y: boolean | number): void; 58} 59 60class IncorrectMultipleParamMismatch extends BaseClass4 { 61 setValues(x: string, y: boolean): void {// error 2 62 console.log(x, y); 63 } 64} 65 66abstract class BaseClass5 { 67 abstract transform(data: number | string): number; 68} 69 70class IncorrectBothMismatch extends BaseClass5 { 71 transform(data: number): number | string {// error 2 72 return data > 10 ? data : "too small"; 73 } 74} 75 76//legal 77abstract class BaseClass { 78 abstract getData(a: string | number): string | number; 79} 80 81class CorrectWiderParam extends BaseClass { 82 getData(a: string | number | boolean): string | number { 83 return typeof a === 'boolean' ? 0 : a; 84 } 85} 86 87class CorrectNarrowerReturn extends BaseClass { 88 getData(a: string | number): string { 89 return typeof a === 'number' ? a.toString() : a; 90 } 91} 92 93class CorrectBothWiderParamNarrowReturn extends BaseClass { 94 getData(a: string | number | boolean): string { 95 return String(a); 96 } 97} 98 99 100class A1 { 101 a: number = 0 102} 103class B1 { 104 a: number = 0 105} 106class C { 107 a: number = 0 108} 109 110class Base { 111 foo(obj: A1 | B1): void { 112 console.log("base") 113 } 114 foo2(obj: A1 | B1): void { 115 console.log("base") 116 } 117 foo3(obj: A1 | B1 | C): void { 118 console.log("base") 119 } 120} 121 122// extends 123class Derived extends Base { 124 foo(obj: A1): void { // error 1 125 console.log("Derived:" + obj.a) 126 } 127 foo2(): void { // error 1 128 console.log("Derived:") 129 } 130 foo3(obj: A1 | B1): void { // error 1 131 console.log("Derived:") 132 } 133} 134 135interface BaseI { 136 foo(obj: A1 | B1):void; 137 foo2(obj: A1): void; 138 foo3(obj: A1 | B1 | C): void; 139} 140 141// implements 142class Derived2 implements BaseI { 143 foo(obj: A1): void { // error 1 144 console.log("Drived"); 145 } 146 foo2(): void { // error 1 147 console.log("Drived"); 148 } 149 foo3(obj: A1 | B1): void { // error 1 150 console.log("Drived"); 151 } 152} 153 154class Base2 { 155 foo(): A1|B1 { 156 console.log("base") 157 return new A1(); 158 } 159 foo2(){ 160 console.log("base") 161 // return new A(); 162 } 163 foo3(): A1 { 164 console.log("base") 165 return new A1(); 166 } 167 foo4():void{ 168 console.log("base") 169 // return new A(); 170 } 171} 172 173//extends 174class Derived3 extends Base2 { 175 foo(): A1|B1|C{ // error 1 176 console.log("Derived:") 177 return new A1(); 178 } 179 180 foo2(): A1{ // error 1 181 console.log("Derived:") 182 return new A1(); 183 } 184 185 foo3(): A1|B1 { // error 1 186 console.log("Derived:") 187 return new A1(); 188 } 189 foo4(): A1{ // error 1 190 console.log("Derived:") 191 return new A1(); 192 } 193} 194 195 196interface Base3 { 197 foo(): A1|B1 ; 198 foo2(): void; 199 foo3(): A1; 200} 201 202// implements 203class Derived4 implements Base3 { 204 foo(): A1|B1|C{ // error 1 205 console.log("Derived:") 206 return new A1(); 207 } 208 209 foo2(): A1{ // error 1 210 console.log("Derived:") 211 return new A1(); 212 } 213 214 foo3(): A1|B1 { // error 1 215 console.log("Derived:") 216 return new A1(); 217 } 218} 219 220class P { 221} 222 223class PP extends P { 224 public static toString(result: number): string { // Legal 225 return ''; 226 } 227} 228 229class RCP<T = undefined> {} 230 231interface aa { 232 aa?: Function; 233} 234interface bb<T = undefined> extends aa { 235 onError(res: RCP<T>): void; 236} 237class CC implements bb { 238 onError(res: RCP): void {} // Legal 239 aa?: Function = () => {} 240} 241class Animal {} 242class Dog extends Animal {} 243 244class Base6 { 245 public foo(): Animal { 246 console.log("base") 247 } 248} 249// extends 250class Derived6 extends Base6 { 251 public foo(): Dog { // no error 252 console.log("Derived:") 253 } 254}