/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ class C { constructor (p: T) { this.v = p; } v: T; } type Alias1Union = T1 | T2; type Alias2Union = C | T2; type Alias3Union = C[] | T2; type Alias4Union = (p: T1) => double | T2; function main() { let v1 : double | double = new Int(1); // primitive double let v2 : double | string = 2; // boxed Double|String let v3 : Alias1Union = new Int(3); // primitive double assert(v3 == 3); let v4 : Alias1Union = 4; // boxed Double|String assert(v4 == 4); let v5 : Alias2Union = new C(5); // C|String let v6 : C = v5 as C; assert(v6.v == 5); let v7 : Alias3Union = [new C(6)]; // C[]|String let v9 : C[] = v7 as C[]; assert (v9[0].v == 6); let v10 : Alias4Union = (p : double) : double => { return p; }; // (p: Double)=>Double |String let v11 : (p : double) => double = v10 as (p : double) => double; assert (v11(7) == 7); }