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/enums/enumBasics.ts === 20declare function AssertType(value:any, type:string):void; 21// Enum without initializers have first member = 0 and successive members = N + 1 22enum E1 { 23 A, 24 B, 25 C 26} 27 28// Enum type is a subtype of Number 29let x: number = E1.A; 30AssertType(x, "number"); 31AssertType(E1.A, "E1.A"); 32 33// Enum object type is anonymous with properties of the enum type and numeric indexer 34let e = E1; 35AssertType(e, "typeof E1"); 36AssertType(E1, "typeof E1"); 37 38let e: { 39AssertType(e, "typeof E1"); 40 41 readonly A: E1.A; 42AssertType(A, "E1.A"); 43AssertType(E1, "any"); 44 45 readonly B: E1.B; 46AssertType(B, "E1.B"); 47AssertType(E1, "any"); 48 49 readonly C: E1.C; 50AssertType(C, "E1.C"); 51AssertType(E1, "any"); 52 53 readonly [n: number]: string; 54AssertType(n, "number"); 55 56}; 57let e: typeof E1; 58AssertType(e, "typeof E1"); 59AssertType(E1, "typeof E1"); 60 61// Reverse mapping of enum returns string name of property 62let s = E1[e.A]; 63AssertType(s, "string"); 64AssertType(E1[e.A], "string"); 65AssertType(E1, "typeof E1"); 66AssertType(e.A, "E1.A"); 67 68let s: string; 69AssertType(s, "string"); 70 71 72// Enum with only constant members 73enum E2 { 74 A = 1, B = 2, C = 3 75} 76 77// Enum with only computed members 78enum E3 { 79 X = 'foo'.length, Y = 4 + 3, Z = +'foo' 80} 81 82// Enum with constant members followed by computed members 83enum E4 { 84 X = 0, Y, Z = 'foo'.length 85} 86 87// Enum with > 2 constant members with no initializer for first member, non zero initializer for second element 88enum E5 { 89 A, 90 B = 3, 91 C // 4 92} 93 94enum E6 { 95 A, 96 B = 0, 97 C // 1 98} 99 100// Enum with computed member initializer of type 'any' 101enum E7 { 102 A = 'foo'['foo'] 103} 104 105// Enum with computed member initializer of type number 106enum E8 { 107 B = 'foo'['foo'] 108} 109 110//Enum with computed member intializer of same enum type 111enum E9 { 112 A, 113 B = A 114} 115 116// (refer to .js to validate) 117// Enum constant members are propagated 118let doNotPropagate = [ 119AssertType(doNotPropagate, "(union)[]"); 120AssertType([ E8.B, E7.A, E4.Z, E3.X, E3.Y, E3.Z], "(union)[]"); 121 122 E8.B, E7.A, E4.Z, E3.X, E3.Y, E3.Z 123AssertType(E8.B, "E8"); 124AssertType(E7.A, "E7"); 125AssertType(E4.Z, "E4.Z"); 126AssertType(E3.X, "E3.X"); 127AssertType(E3.Y, "E3.Y"); 128AssertType(E3.Z, "E3.Z"); 129 130]; 131// Enum computed members are not propagated 132let doPropagate = [ 133AssertType(doPropagate, "(union)[]"); 134AssertType([ E9.A, E9.B, E6.B, E6.C, E6.A, E5.A, E5.B, E5.C], "(union)[]"); 135 136 E9.A, E9.B, E6.B, E6.C, E6.A, E5.A, E5.B, E5.C 137AssertType(E9.A, "E9"); 138AssertType(E9.B, "E9"); 139AssertType(E6.B, "E6.A"); 140AssertType(E6.C, "E6.C"); 141AssertType(E6.A, "E6.A"); 142AssertType(E5.A, "E5.A"); 143AssertType(E5.B, "E5.B"); 144AssertType(E5.C, "E5.C"); 145 146]; 147 148 149 150