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 16export const tsNumber = 1; 17export const tsString = 'string'; 18interface AnyTypeMethod<T> { 19 get(a: T): T; 20} 21 22export class AnyTypeMethodClass<T> implements AnyTypeMethod<T> { 23 get(a: T): T { 24 return a; 25 } 26} 27 28export function createInterfaceClassAnyTypeMethod(): anyTypeMethodClass<unknown> { 29 return new AnyTypeMethodClass<unknown>(); 30} 31 32type UnionType = string | number; 33 34interface UnionTypeMethod { 35 get(a: UnionType): UnionType; 36} 37 38export class UnionTypeMethodClass implements UnionTypeMethod { 39 get(a: UnionType): UnionType { 40 return a; 41 } 42} 43 44export function createInterfaceClassUnionTypeMethod(): anyTypeMethodClass<number> { 45 return new AnyTypeMethodClass<number>(); 46} 47 48interface ArgInterface { 49 get(): number; 50} 51 52export function subsetByRefInterface(obj: ArgInterface): number { 53 return obj.get(); 54} 55 56class UserClass { 57 public value = 1; 58} 59 60interface SubsetByValue { 61 get(): UserClass; 62} 63 64export class SubsetByValueClass implements SubsetByValue { 65 get(): UserClass { 66 return new UserClass(); 67 } 68} 69 70export function createSubsetByValueClassFromTs(): SubsetByValueClass { 71 return new SubsetByValueClass(); 72} 73 74interface OptionalMethod { 75 getNum?(): number; 76 getStr(): string; 77} 78 79export class WithOptionalMethodClass implements OptionalMethod { 80 getNum(): number { 81 return tsNumber; 82 } 83 84 getStr(): string { 85 return tsString; 86 } 87} 88 89export class WithoutOptionalMethodClass implements OptionalMethod { 90 getStr(): string { 91 return tsString; 92 } 93} 94 95export function createClassWithOptionalMethod(): WithOptionalMethodClass { 96 return new WithOptionalMethodClass(); 97} 98 99export function createClassWithoutOptionalMethod(): WithoutOptionalMethodClass { 100 return new WithoutOptionalMethodClass(); 101} 102 103interface OptionalArgResult { 104 with: WithOptionalMethodClass; 105 without?: WithoutOptionalMethodClass; 106} 107 108export function optionalArg(arg: WithOptionalMethodClass, optional?: WithoutOptionalMethodClass): OptionalArgResult { 109 if (optional) { 110 return { with: arg, without: optional }; 111 } 112 113 return { with: arg }; 114} 115 116type ArrayArg = (WithOptionalMethodClass | WithoutOptionalMethodClass)[]; 117 118export function optionalArgArray(...arg: ArrayArg): OptionalArgResult { 119 const withOptional = arg[0] as WithOptionalMethodClass; 120 const withoutOptional = arg[1] as WithoutOptionalMethodClass; 121 122 if (withoutOptional) { 123 return { with: withOptional, without: withoutOptional }; 124 } 125 126 return { with: withOptional }; 127} 128 129interface TupleType { 130 get(arg: [number, string]): [number, string]; 131} 132 133export class TupleTypeMethodClass implements TupleType { 134 get(arg: [number, string]): [number, string] { 135 return arg; 136 } 137} 138 139export function createInterfaceClassTupleTypeMethodFromTs(): TupleTypeMethodClass { 140 return new TupleTypeMethodClass(); 141} 142 143export const unionTypeMethodInstanceClass = new UnionTypeMethodClass(); 144export const subsetByValueInstanceClass = new SubsetByValueClass(); 145export const tupleInstanceClass = new TupleTypeMethodClass(); 146export const withoutOptionalMethodInstanceClass = new WithoutOptionalMethodClass(); 147export const withOptionalMethodInstanceClass = new WithOptionalMethodClass(); 148