13af6ab5fSopenharmony_ci/* 23af6ab5fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_cifunction foo(x: "a"|"b"|"c") { 173af6ab5fSopenharmony_ci x = "c" 183af6ab5fSopenharmony_ci return x 193af6ab5fSopenharmony_ci} 203af6ab5fSopenharmony_ci 213af6ab5fSopenharmony_cifunction id<T>(v: Object): T { 223af6ab5fSopenharmony_ci return v as T 233af6ab5fSopenharmony_ci} 243af6ab5fSopenharmony_ci 253af6ab5fSopenharmony_cifunction getColor(colors: Array<"default"|"invisible"|number>, id: number) { 263af6ab5fSopenharmony_ci return colors[id] 273af6ab5fSopenharmony_ci} 283af6ab5fSopenharmony_ci 293af6ab5fSopenharmony_cifunction f1(a: "xyz"): "xyz" { 303af6ab5fSopenharmony_ci return a 313af6ab5fSopenharmony_ci} 323af6ab5fSopenharmony_ci 333af6ab5fSopenharmony_cifunction f2(a: "aa"|"bb"): "aa"|"bb" { 343af6ab5fSopenharmony_ci return a 353af6ab5fSopenharmony_ci} 363af6ab5fSopenharmony_ci 373af6ab5fSopenharmony_cifunction f3(a: "aa"|"bb") { 383af6ab5fSopenharmony_ci return a + "cc" 393af6ab5fSopenharmony_ci} 403af6ab5fSopenharmony_ci 413af6ab5fSopenharmony_cifunction f4(a: "b"): "b" { 423af6ab5fSopenharmony_ci return a 433af6ab5fSopenharmony_ci} 443af6ab5fSopenharmony_ci 453af6ab5fSopenharmony_ciclass A { 463af6ab5fSopenharmony_ci p: "aa"|"bb" = "bb" 473af6ab5fSopenharmony_ci} 483af6ab5fSopenharmony_ci 493af6ab5fSopenharmony_cifunction test_inference_from_return() { 503af6ab5fSopenharmony_ci let arr = new Array<"default"|"invisible"|number>(3); 513af6ab5fSopenharmony_ci arr[0] = "default" 523af6ab5fSopenharmony_ci arr[1] = 42 533af6ab5fSopenharmony_ci arr[2] = "invisible" 543af6ab5fSopenharmony_ci let s = getColor(arr, 0) // s is of type string|number 553af6ab5fSopenharmony_ci assert s == "default" 563af6ab5fSopenharmony_ci s = "dd" 573af6ab5fSopenharmony_ci} 583af6ab5fSopenharmony_ci 593af6ab5fSopenharmony_cifunction test_inference_from_conditional(cond: boolean) { 603af6ab5fSopenharmony_ci let x1 = cond ? "aa" : "bb" // type of x1 is string 613af6ab5fSopenharmony_ci assert x1 == "aa" 623af6ab5fSopenharmony_ci x1 = "abc" 633af6ab5fSopenharmony_ci assert x1 == "abc" 643af6ab5fSopenharmony_ci let x2: "yes"|"no" = cond ? "yes" : "no" 653af6ab5fSopenharmony_ci assert x2 == "yes" 663af6ab5fSopenharmony_ci let x3 = cond ? "one" : new A // type of x3 is string|A 673af6ab5fSopenharmony_ci assert x3 == "one" 683af6ab5fSopenharmony_ci x3 = "bb" 693af6ab5fSopenharmony_ci assert x3 == "bb" 703af6ab5fSopenharmony_ci 713af6ab5fSopenharmony_ci const x4 = cond ? "aa" : "bb" // type of x4 is "aa"|"bb" 723af6ab5fSopenharmony_ci let y4 = f2(x4) // type of y4 is "aa"|"bb" 733af6ab5fSopenharmony_ci let z4 = f2(y4) 743af6ab5fSopenharmony_ci assert z4 == "aa" 753af6ab5fSopenharmony_ci 763af6ab5fSopenharmony_ci let x5 = ((p: boolean) => p ? "bb" : "aa")(cond) // type of x5 is string 773af6ab5fSopenharmony_ci assert x5 == "bb" 783af6ab5fSopenharmony_ci x5 = "xyz" 793af6ab5fSopenharmony_ci assert x5 == "xyz" 803af6ab5fSopenharmony_ci} 813af6ab5fSopenharmony_ci 823af6ab5fSopenharmony_cifunction main() { 833af6ab5fSopenharmony_ci const x1 = "xyz" 843af6ab5fSopenharmony_ci let y1 = f1(x1) 853af6ab5fSopenharmony_ci assert y1 == "xyz" 863af6ab5fSopenharmony_ci 873af6ab5fSopenharmony_ci let s = id<"a"|"b"|"c">("b") // type of s is "a"|"b"|"c" 883af6ab5fSopenharmony_ci assert s == "b" 893af6ab5fSopenharmony_ci assert s != "a" 903af6ab5fSopenharmony_ci assert foo(s) == "c" 913af6ab5fSopenharmony_ci let y = s as "b" 923af6ab5fSopenharmony_ci assert y == "b" 933af6ab5fSopenharmony_ci assert f4(s as "b") == "b" 943af6ab5fSopenharmony_ci 953af6ab5fSopenharmony_ci test_inference_from_conditional(true) 963af6ab5fSopenharmony_ci test_inference_from_return() 973af6ab5fSopenharmony_ci 983af6ab5fSopenharmony_ci assert f3("aa") == "aacc" 993af6ab5fSopenharmony_ci 1003af6ab5fSopenharmony_ci let a = new A 1013af6ab5fSopenharmony_ci let x2 = a.p 1023af6ab5fSopenharmony_ci assert f2(x2) == "bb" 1033af6ab5fSopenharmony_ci let z = x2 1043af6ab5fSopenharmony_ci assert f2(z) == "bb" 1053af6ab5fSopenharmony_ci 1063af6ab5fSopenharmony_ci let x3: ("aa"|"bb")[] = ["aa", "bb", "aa"] 1073af6ab5fSopenharmony_ci let y3 = f2(x3[0]) // type of y3 is "aa"|"bb" 1083af6ab5fSopenharmony_ci assert y3 == "aa" 1093af6ab5fSopenharmony_ci assert x3[1] == "bb" 1103af6ab5fSopenharmony_ci 1113af6ab5fSopenharmony_ci let x4 = ["aa", "bb", "aa", 43] // type of x4 is (string|int)[] 1123af6ab5fSopenharmony_ci x4[0] = "cc" 1133af6ab5fSopenharmony_ci assert x4[0] == "cc" 1143af6ab5fSopenharmony_ci assert x4[1] == "bb" 1153af6ab5fSopenharmony_ci x4[2] = 55 1163af6ab5fSopenharmony_ci x4[3] = "xyz" 1173af6ab5fSopenharmony_ci assert x4[2] == 55 1183af6ab5fSopenharmony_ci assert x4[3] == "xyz" 1193af6ab5fSopenharmony_ci} 120