• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16// Basic type
17export function processInput(input: string): string;
18export function processInput(input: number): number;
19export function processInput(input: boolean): boolean;
20export function processInput(input: string | number | boolean): string | number | boolean {
21  if (typeof input === 'string') {
22    return input.toUpperCase();
23  } else if (typeof input === 'number') {
24    return input * 2;
25  } else {
26    return !input;
27  }
28}
29
30// Generic
31export function createArray<T>(value: T, length: number): T[];
32export function createArray<T, U>(value1: T, value2: U, length: number): (T | U)[];
33export function createArray<T, U>(
34  value: T,
35  value2OrLength: number | U,
36  length?: number
37): T[] | (T | U)[] {
38  if (typeof length === 'number') {
39    const value2 = value2OrLength as U;
40    return Array.from({ length }, (_, i) => i % 2 === 0 ? value : value2);
41  } else {
42    const len = value2OrLength as number;
43    return Array(len).fill(value);
44  }
45}
46
47// Object
48export interface User {
49  id: number;
50  name: string;
51}
52
53export interface Admin {
54  id: number;
55  role: string;
56}
57
58export function getUser(id: number): User;
59export function getUser(role: string): Admin;
60export function getUser(idOrRole: number | string): User | Admin;
61export function getUser(idOrRole: number | string): User | Admin {
62  if (typeof idOrRole === 'number') {
63    return { id: idOrRole, name: 'John Doe' };
64  } else {
65    return { id: 1, role: idOrRole };
66  }
67}
68
69export class TestClass {
70  name: string = 'aaa';
71  age: number = 24;
72}
73
74export class UnionPropClass {
75  unionProp: number | string | object | TestClass | boolean = 1;
76}
77
78export let dynamicObj: UnionPropClass = new UnionPropClass();
79
80
81export class Base {
82  baseVal: number = 1;
83  getCurrent() {
84      return this;
85  }
86}
87
88export class Child extends Base {
89  childVal: number = 2;
90  getSuper() {
91      return super.getCurrent();
92  }
93}
94
95export let baseFunc = new Base().getCurrent;
96export let childFunc = new Child().getSuper;
97
98export let fooBaseObj = {
99  foo: baseFunc,
100  fooFunc: function() {
101      return this.foo();
102  }
103};
104export let fooChildObj = {
105  foo: childFunc,
106  fooFunc: function() {
107      return this.foo();
108  }
109};
110
111
112export let arrowFunc = () => {
113  return this;
114};
115
116export function foo1(a: number, b: string, c?: boolean, d: number = 1): boolean {
117  return a === 1 && b === 'str' && c === undefined && d === 1;
118}
119
120export function foo2(a: number, b: string, c?: boolean, d: number = 1): boolean {
121  return a === 1 && b === 'str' && c === true && d === 1;
122}
123
124export function foo3(a: number, b: string, c?: boolean, d: number = 1): boolean {
125  return a === 1 && b === 'str' && c === true && d === 2;
126}
127
128export function fun1(a: number, b: string, c: boolean = true, d?: number): boolean {
129  return a === 1 && b === 'str' && c === true && d === undefined;
130}
131
132export function fun2(a: number, b: string, c: boolean = true, d?: number): boolean {
133  return a === 1 && b === 'str' && c === false && d === undefined;
134}
135
136export function fun3(a: number, b: string, c: boolean = true, d?: number): boolean {
137  return a === 1 && b === 'str' && c === false && d === 2;
138}
139
140// errors
141export let err: Error = new Error('This is a Error');
142
143export function ErrorFunc(message: string): void {
144  throw new Error(message);
145}
146
147export class CustomError extends Error {
148  code: number;
149  constructor(message: string, code: number) {
150    super(message);
151    this.name = 'CustomError';
152    this.code = code;
153  }
154}
155
156export function CustomErrorFunc(message: string, code: number): void {
157  throw new CustomError(message, code);
158}
159