1# Indexed access is not supported for fields 2 3Rule ``arkts-no-props-by-index`` 4 5**Severity: error** 6 7ArkTS does not support dynamic field declaration and access. Declare all 8object fields immediately in the class. Access only those class fields 9that are either declared in the class, or accessible via inheritance. Accessing 10any other fields is prohibited, and causes compile-time errors. 11 12To access a field, use ``obj.field`` syntax, indexed access (``obj["field"]``) 13is not supported. An exception are: 14 15- All typed arrays from the standard library (for example, ``Int32Array``), which 16support access to their elements through ``container[index]`` syntax. 17- Tuples. 18- Records. 19- Enums. 20 21 22## TypeScript 23 24 25``` 26 27 class Point { 28 x: number = 0 29 y: number = 0 30 } 31 let p: Point = {x: 1, y: 2} 32 console.log(p["x"]) 33 34 class Person { 35 name: string = "" 36 age: number = 0; // semicolon is required here 37 [key: string]: string | number 38 } 39 40 let person: Person = { 41 name: "John", 42 age: 30, 43 email: "***@example.com", 44 phoneNumber: "18*********", 45 } 46 47``` 48 49## ArkTS 50 51 52``` 53 54 class Point { 55 x: number = 0 56 y: number = 0 57 } 58 let p: Point = {x: 1, y: 2} 59 console.log(p.x) 60 61 class Person { 62 name: string 63 age: number 64 email: string 65 phoneNumber: string 66 67 constructor(name: string, age: number, email: string, 68 phoneNumber: string) { 69 this.name = name 70 this.age = age 71 this.email = email 72 this.phoneNumber = phoneNumber 73 } 74 } 75 76 let person = new Person("John", 30, "***@example.com", "18*********") 77 console.log(person["name"]) // Compile-time error 78 console.log(person.unknownProperty) // Compile-time error 79 80 let arr = new Int32Array(1) 81 console.log(arr[0]) 82 83``` 84 85 86