1/* 2* Copyright (c) Microsoft Corporation. All rights reserved. 3* Copyright (c) 2023 Huawei Device Co., Ltd. 4* Licensed under the Apache License, Version 2.0 (the "License"); 5* you may not use this file except in compliance with the License. 6* You may obtain a copy of the License at 7* 8* http://www.apache.org/licenses/LICENSE-2.0 9* 10* Unless required by applicable law or agreed to in writing, software 11* distributed under the License is distributed on an "AS IS" BASIS, 12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13* See the License for the specific language governing permissions and 14* limitations under the License. 15* 16* This file has been modified by Huawei to verify type inference by adding verification statements. 17*/ 18 19// === tests/cases/conformance/types/literal/literalTypes2.ts === 20declare function AssertType(value:any, type:string):void; 21enum E { 22 A, B, C 23} 24 25let cond: boolean; 26AssertType(cond, "boolean"); 27 28function f1(p1 = 1, p2 = "abc", p3 = true, p4 = E.A) { 29 let v1 = 1; 30AssertType(v1, "number"); 31AssertType(1, "int"); 32 33 let v2 = -123; 34AssertType(v2, "number"); 35AssertType(-123, "int"); 36AssertType(123, "int"); 37 38 let v3 = 3 + 4; 39AssertType(v3, "number"); 40AssertType(3 + 4, "number"); 41AssertType(3, "int"); 42AssertType(4, "int"); 43 44 let v4 = "abc"; 45AssertType(v4, "string"); 46AssertType("abc", "string"); 47 48 let v5 = ""; 49AssertType(v5, "string"); 50AssertType("", "string"); 51 52 let v6 = "abc" + "def"; 53AssertType(v6, "string"); 54AssertType("abc" + "def", "string"); 55AssertType("abc", "string"); 56AssertType("def", "string"); 57 58 let v7 = true; 59AssertType(v7, "boolean"); 60AssertType(true, "boolean"); 61 62 let v8 = E.A; 63AssertType(v8, "E"); 64AssertType(E.A, "E.A"); 65 66 let x1 = 1; 67AssertType(x1, "number"); 68AssertType(1, "int"); 69 70 let x2 = -123; 71AssertType(x2, "number"); 72AssertType(-123, "int"); 73AssertType(123, "int"); 74 75 let x3 = 3 + 4; 76AssertType(x3, "number"); 77AssertType(3 + 4, "number"); 78AssertType(3, "int"); 79AssertType(4, "int"); 80 81 let x4 = "abc"; 82AssertType(x4, "string"); 83AssertType("abc", "string"); 84 85 let x5 = ""; 86AssertType(x5, "string"); 87AssertType("", "string"); 88 89 let x6 = "abc" + "def"; 90AssertType(x6, "string"); 91AssertType("abc" + "def", "string"); 92AssertType("abc", "string"); 93AssertType("def", "string"); 94 95 let x7 = true; 96AssertType(x7, "boolean"); 97AssertType(true, "boolean"); 98 99 let x8 = E.A; 100AssertType(x8, "E"); 101AssertType(E.A, "E.A"); 102 103 const c1 = 1; 104AssertType(c1, "int"); 105AssertType(1, "int"); 106 107 const c2 = -123; 108AssertType(c2, "int"); 109AssertType(-123, "int"); 110AssertType(123, "int"); 111 112 const c3 = 3 + 4; 113AssertType(c3, "number"); 114AssertType(3 + 4, "number"); 115AssertType(3, "int"); 116AssertType(4, "int"); 117 118 const c4 = "abc"; 119AssertType(c4, "string"); 120AssertType("abc", "string"); 121 122 const c5 = ""; 123AssertType(c5, "string"); 124AssertType("", "string"); 125 126 const c6 = "abc" + "def"; 127AssertType(c6, "string"); 128AssertType("abc" + "def", "string"); 129AssertType("abc", "string"); 130AssertType("def", "string"); 131 132 const c7 = true; 133AssertType(c7, "boolean"); 134AssertType(true, "boolean"); 135 136 const c8 = E.A; 137AssertType(c8, "E.A"); 138AssertType(E.A, "E.A"); 139} 140 141function f2(p1: 1 = 1, p2: "abc" = "abc", p3: true = true, p4: E.A = E.A) { 142 let v1: 1 = 1; 143AssertType(v1, "int"); 144AssertType(1, "int"); 145 146 let v2: -123 = -123; 147AssertType(v2, "int"); 148AssertType(-123, "int"); 149AssertType(123, "int"); 150AssertType(-123, "int"); 151AssertType(123, "int"); 152 153 let v3: "abc" = "abc"; 154AssertType(v3, "string"); 155AssertType("abc", "string"); 156 157 let v4: true = true; 158AssertType(v4, "boolean"); 159AssertType(true, "boolean"); 160AssertType(true, "boolean"); 161 162 let v5: E.A = E.A; 163AssertType(v5, "E.A"); 164AssertType(E, "any"); 165AssertType(E.A, "E.A"); 166 167 let x1: 1 = 1; 168AssertType(x1, "int"); 169AssertType(1, "int"); 170 171 let x2: -123 = -123; 172AssertType(x2, "int"); 173AssertType(-123, "int"); 174AssertType(123, "int"); 175AssertType(-123, "int"); 176AssertType(123, "int"); 177 178 let x3: "abc" = "abc"; 179AssertType(x3, "string"); 180AssertType("abc", "string"); 181 182 let x4: true = true; 183AssertType(x4, "boolean"); 184AssertType(true, "boolean"); 185AssertType(true, "boolean"); 186 187 let x5: E.A = E.A; 188AssertType(x5, "E.A"); 189AssertType(E, "any"); 190AssertType(E.A, "E.A"); 191} 192 193function f3() { 194 const c1 = cond ? 1 : 2; 195AssertType(c1, "union"); 196AssertType(cond ? 1 : 2, "union"); 197AssertType(cond, "boolean"); 198AssertType(1, "int"); 199AssertType(2, "int"); 200 201 const c2 = cond ? 1 : "two"; 202AssertType(c2, "union"); 203AssertType(cond ? 1 : "two", "union"); 204AssertType(cond, "boolean"); 205AssertType(1, "int"); 206AssertType("two", "string"); 207 208 const c3 = cond ? E.A : cond ? true : 123; 209AssertType(c3, "union"); 210AssertType(cond ? E.A : cond ? true : 123, "union"); 211AssertType(cond, "boolean"); 212AssertType(E.A, "E.A"); 213AssertType(cond ? true : 123, "union"); 214AssertType(cond, "boolean"); 215AssertType(true, "boolean"); 216AssertType(123, "int"); 217 218 const c4 = cond ? "abc" : null; 219AssertType(c4, "string"); 220AssertType(cond ? "abc" : null, "string"); 221AssertType(cond, "boolean"); 222AssertType("abc", "string"); 223AssertType(null, "null"); 224 225 const c5 = cond ? 456 : undefined; 226AssertType(c5, "int"); 227AssertType(cond ? 456 : undefined, "int"); 228AssertType(cond, "boolean"); 229AssertType(456, "int"); 230AssertType(undefined, "undefined"); 231 232 const c6: { kind: 123 } = { kind: 123 }; 233AssertType(c6, "{ kind: 123; }"); 234AssertType(kind, "int"); 235AssertType({ kind: 123 }, "{ kind: 123; }"); 236AssertType(kind, "int"); 237AssertType(123, "int"); 238 239 const c7: [1 | 2, "foo" | "bar"] = [1, "bar"]; 240AssertType(c7, "[union, union]"); 241AssertType([1, "bar"], "[1, "bar"]"); 242AssertType(1, "int"); 243AssertType("bar", "string"); 244 245 const c8 = cond ? c6 : cond ? c7 : "hello"; 246AssertType(c8, "union"); 247AssertType(cond ? c6 : cond ? c7 : "hello", "union"); 248AssertType(cond, "boolean"); 249AssertType(c6, "{ kind: 123; }"); 250AssertType(cond ? c7 : "hello", "union"); 251AssertType(cond, "boolean"); 252AssertType(c7, "[union, union]"); 253AssertType("hello", "string"); 254 255 let x1 = c1; 256AssertType(x1, "number"); 257AssertType(c1, "union"); 258 259 let x2 = c2; 260AssertType(x2, "union"); 261AssertType(c2, "union"); 262 263 let x3 = c3; 264AssertType(x3, "union"); 265AssertType(c3, "union"); 266 267 let x4 = c4; 268AssertType(x4, "string"); 269AssertType(c4, "string"); 270 271 let x5 = c5; 272AssertType(x5, "number"); 273AssertType(c5, "int"); 274 275 let x6 = c6; 276AssertType(x6, "{ kind: 123; }"); 277AssertType(c6, "{ kind: 123; }"); 278 279 let x7 = c7; 280AssertType(x7, "[union, union]"); 281AssertType(c7, "[union, union]"); 282 283 let x8 = c8; 284AssertType(x8, "union"); 285AssertType(c8, "union"); 286} 287 288class C1 { 289 x1 = 1; 290 x2 = -123; 291 x3 = 3 + 4; 292 x4 = "abc"; 293 x5 = ""; 294 x6 = "abc" + "def"; 295 x7 = true; 296 x8 = E.A; 297 readonly c1 = 1; 298 readonly c2 = -123; 299 readonly c3 = 3 + 4; 300 readonly c4 = "abc"; 301 readonly c5 = ""; 302 readonly c6 = "abc" + "def"; 303 readonly c7 = true; 304 readonly c8 = E.A; 305} 306 307function f4() { 308 const c1 = { a: 1, b: "foo" }; 309AssertType(c1, "{ a: number; b: string; }"); 310AssertType({ a: 1, b: "foo" }, "{ a: number; b: string; }"); 311AssertType(a, "number"); 312AssertType(1, "int"); 313AssertType(b, "string"); 314AssertType("foo", "string"); 315 316 const c2: { a : 0 | 1, b: "foo" | "bar" } = { a: 1, b: "foo" }; 317AssertType(c2, "{ a: union; b: union; }"); 318AssertType(a, "union"); 319AssertType(b, "union"); 320AssertType({ a: 1, b: "foo" }, "{ a: 1; b: "foo"; }"); 321AssertType(a, "int"); 322AssertType(1, "int"); 323AssertType(b, "string"); 324AssertType("foo", "string"); 325 326 let x1 = { a: 1, b: "foo" }; 327AssertType(x1, "{ a: number; b: string; }"); 328AssertType({ a: 1, b: "foo" }, "{ a: number; b: string; }"); 329AssertType(a, "number"); 330AssertType(1, "int"); 331AssertType(b, "string"); 332AssertType("foo", "string"); 333 334 let x2: { a : 0 | 1, b: "foo" | "bar" } = { a: 1, b: "foo" }; 335AssertType(x2, "{ a: union; b: union; }"); 336AssertType(a, "union"); 337AssertType(b, "union"); 338AssertType({ a: 1, b: "foo" }, "{ a: 1; b: "foo"; }"); 339AssertType(a, "int"); 340AssertType(1, "int"); 341AssertType(b, "string"); 342AssertType("foo", "string"); 343} 344 345function f5() { 346 const c1 = [1, "foo"]; 347AssertType(c1, "(union)[]"); 348AssertType([1, "foo"], "(union)[]"); 349AssertType(1, "int"); 350AssertType("foo", "string"); 351 352 const c2: (1 | "foo")[] = [1, "foo"]; 353AssertType(c2, "(union)[]"); 354AssertType([1, "foo"], "(union)[]"); 355AssertType(1, "int"); 356AssertType("foo", "string"); 357 358 const c3: [1, "foo"] = [1, "foo"]; 359AssertType(c3, "[1, "foo"]"); 360AssertType([1, "foo"], "[1, "foo"]"); 361AssertType(1, "int"); 362AssertType("foo", "string"); 363 364 let x1 = [1, "foo"]; 365AssertType(x1, "(union)[]"); 366AssertType([1, "foo"], "(union)[]"); 367AssertType(1, "int"); 368AssertType("foo", "string"); 369 370 let x2: (1 | "foo")[] = [1, "foo"]; 371AssertType(x2, "(union)[]"); 372AssertType([1, "foo"], "(union)[]"); 373AssertType(1, "int"); 374AssertType("foo", "string"); 375 376 let x3: [1, "foo"] = [1, "foo"]; 377AssertType(x3, "[1, "foo"]"); 378AssertType([1, "foo"], "[1, "foo"]"); 379AssertType(1, "int"); 380AssertType("foo", "string"); 381} 382 383function f6() { 384 const { c1 = true, c2 = 0, c3 = "foo" } = { c1: false, c2: 1, c3: "bar" }; 385AssertType(c1, "boolean"); 386AssertType(true, "boolean"); 387AssertType(c2, "union"); 388AssertType(0, "int"); 389AssertType(c3, "union"); 390AssertType("foo", "string"); 391AssertType({ c1: false, c2: 1, c3: "bar" }, "{ c1?: false; c2?: 1; c3?: "bar"; }"); 392AssertType(c1, "boolean"); 393AssertType(false, "boolean"); 394AssertType(c2, "int"); 395AssertType(1, "int"); 396AssertType(c3, "string"); 397AssertType("bar", "string"); 398 399 let { x1 = true, x2 = 0, x3 = "foo" } = { x1: false, x2: 1, x3: "bar" }; 400AssertType(x1, "boolean"); 401AssertType(true, "boolean"); 402AssertType(x2, "number"); 403AssertType(0, "int"); 404AssertType(x3, "string"); 405AssertType("foo", "string"); 406AssertType({ x1: false, x2: 1, x3: "bar" }, "{ x1?: false; x2?: number; x3?: string; }"); 407AssertType(x1, "boolean"); 408AssertType(false, "boolean"); 409AssertType(x2, "number"); 410AssertType(1, "int"); 411AssertType(x3, "string"); 412AssertType("bar", "string"); 413} 414 415function f10() { 416AssertType("hello", "string"); 417 return "hello"; 418} 419 420function f11() { 421AssertType(cond ? 1 : "two", "union"); 422AssertType(cond, "boolean"); 423AssertType(1, "int"); 424AssertType("two", "string"); 425 return cond ? 1 : "two"; 426} 427 428function f12() { 429 if (cond) { 430AssertType(cond, "boolean"); 431 432AssertType(1, "int"); 433 return 1; 434 } 435 else { 436AssertType("two", "string"); 437 return "two"; 438 } 439} 440 441class C2 { 442 foo() { 443AssertType(0, "int"); 444 return 0; 445 } 446 bar() { 447AssertType(cond ? 0 : 1, "union"); 448AssertType(cond, "boolean"); 449AssertType(0, "int"); 450AssertType(1, "int"); 451 return cond ? 0 : 1; 452 } 453} 454 455function f20() { 456 const f1 = () => 0; 457AssertType(f1, "() => number"); 458AssertType(() => 0, "() => number"); 459AssertType(0, "int"); 460 461 const f2 = () => "hello"; 462AssertType(f2, "() => string"); 463AssertType(() => "hello", "() => string"); 464AssertType("hello", "string"); 465 466 const f3 = () => true; 467AssertType(f3, "() => boolean"); 468AssertType(() => true, "() => boolean"); 469AssertType(true, "boolean"); 470 471 const f4 = () => E.C; 472AssertType(f4, "() => E"); 473AssertType(() => E.C, "() => E"); 474AssertType(E.C, "E.C"); 475 476 const f5 = (): "foo" => "foo"; 477AssertType(f5, "() => "foo""); 478AssertType((): "foo" => "foo", "() => "foo""); 479AssertType("foo", "string"); 480 481 const f6: () => "foo" | "bar" = () => "bar"; 482AssertType(f6, "() => union"); 483AssertType(() => "bar", "() => "bar""); 484AssertType("bar", "string"); 485 486 const f7: (() => "foo") | (() => "bar") = () => "bar"; 487AssertType(f7, "union"); 488AssertType(() => "bar", "() => "bar""); 489AssertType("bar", "string"); 490} 491 492declare function g1<T>(x: T): T; 493declare function g2<T>(x: T, y: T): T; 494declare function g3<T, U>(x: T, y: U): T | U; 495declare function g4<T>(x: T): T[]; 496declare function g5<T extends number>(x: T, y: T): T[]; 497declare function g6<T>(x: T[]): T; 498declare function g7<T>(x: T[]): T[]; 499declare function g8<T>(x: T, f: (p: T) => T): T; 500 501const a: (1 | 2)[] = [1, 2]; 502AssertType(a, "(union)[]"); 503AssertType([1, 2], "(union)[]"); 504AssertType(1, "int"); 505AssertType(2, "int"); 506 507const x1 = g1(1); // Type 1 508AssertType(x1, "int"); 509AssertType(g1(1), "int"); 510AssertType(g1, "<T>(T) => T"); 511AssertType(1, "int"); 512 513const x2 = g2(1, 1); // Type 1 514AssertType(x2, "int"); 515AssertType(g2(1, 1), "int"); 516AssertType(g2, "<T>(T, T) => T"); 517AssertType(1, "int"); 518AssertType(1, "int"); 519 520const x3 = g2(1, 2); // Type 1 | 2 521AssertType(x3, "union"); 522AssertType(g2(1, 2), "union"); 523AssertType(g2, "<T>(T, T) => T"); 524AssertType(1, "int"); 525AssertType(2, "int"); 526 527const x4 = g3(1, "two"); // Type 1 | "two" 528AssertType(x4, "union"); 529AssertType(g3(1, "two"), "union"); 530AssertType(g3, "<T, U>(T, U) => union"); 531AssertType(1, "int"); 532AssertType("two", "string"); 533 534const x5 = g4(1); // Type number[] 535AssertType(x5, "number[]"); 536AssertType(g4(1), "number[]"); 537AssertType(g4, "<T>(T) => T[]"); 538AssertType(1, "int"); 539 540const x6 = g5(1, 2); // Type (1 | 2)[] 541AssertType(x6, "(union)[]"); 542AssertType(g5(1, 2), "(union)[]"); 543AssertType(g5, "<T extends number>(T, T) => T[]"); 544AssertType(1, "int"); 545AssertType(2, "int"); 546 547const x7 = g6([1, 2]); // Type number 548AssertType(x7, "number"); 549AssertType(g6([1, 2]), "number"); 550AssertType(g6, "<T>(T[]) => T"); 551AssertType([1, 2], "number[]"); 552AssertType(1, "int"); 553AssertType(2, "int"); 554 555const x8 = g6(a); // Type 1 | 2 556AssertType(x8, "union"); 557AssertType(g6(a), "union"); 558AssertType(g6, "<T>(T[]) => T"); 559AssertType(a, "(union)[]"); 560 561const x9 = g7(a); // Type (1 | 2)[] 562AssertType(x9, "(union)[]"); 563AssertType(g7(a), "(union)[]"); 564AssertType(g7, "<T>(T[]) => T[]"); 565AssertType(a, "(union)[]"); 566 567const x10 = g8(1, x => x); // Type number 568AssertType(x10, "number"); 569AssertType(g8(1, x => x), "number"); 570AssertType(g8, "<T>(T, (T) => T) => T"); 571AssertType(1, "int"); 572AssertType(x => x, "(number) => number"); 573AssertType(x, "number"); 574AssertType(x, "number"); 575 576const x11 = g8(1, x => x + 1); // Type number 577AssertType(x11, "number"); 578AssertType(g8(1, x => x + 1), "number"); 579AssertType(g8, "<T>(T, (T) => T) => T"); 580AssertType(1, "int"); 581AssertType(x => x + 1, "(number) => number"); 582AssertType(x, "number"); 583AssertType(x + 1, "number"); 584AssertType(x, "number"); 585AssertType(1, "int"); 586 587function makeArray<T>(x: T): T[] { 588AssertType([x], "T[]"); 589AssertType(x, "T"); 590 return [x]; 591} 592 593function append<T>(a: T[], x: T): T[] { 594 let result = a.slice(); 595AssertType(result, "T[]"); 596AssertType(a.slice(), "T[]"); 597AssertType(a.slice, "(?number, ?number) => T[]"); 598 599 result.push(x); 600AssertType(result.push(x), "number"); 601AssertType(result.push, "(...T[]) => number"); 602AssertType(x, "T"); 603 604AssertType(result, "T[]"); 605 return result; 606} 607 608type Bit = 0 | 1; 609 610let aa = makeArray<Bit>(0); 611AssertType(aa, "Bit[]"); 612AssertType(makeArray<Bit>(0), "Bit[]"); 613AssertType(makeArray, "<T>(T) => T[]"); 614AssertType(0, "int"); 615 616aa = append(aa, 1); 617AssertType(aa = append(aa, 1), "Bit[]"); 618AssertType(aa, "Bit[]"); 619AssertType(append(aa, 1), "Bit[]"); 620AssertType(append, "<T>(T[], T) => T[]"); 621AssertType(aa, "Bit[]"); 622AssertType(1, "int"); 623 624 625