1# ArkUI Subsystem ChangeLog
2
3## cl.arkui.1 Restrictions on Data Type Declarations of State Variables
4
51. The data types of state variables decorated by state decorators must be explicitly declared. They cannot be declared as **any** or **Date**.
6
7    Example:
8
9    ```ts
10    // xxx.ets
11    @Entry
12    @Component
13    struct DatePickerExample {
14      // Incorrect: @State isLunar: any = false
15      @State isLunar: boolean = false
16      // Incorrect: @State selectedDate: Date = new Date('2021-08-08')
17      private selectedDate: Date = new Date('2021-08-08')
18
19      build() {
20        Column() {
21          Button('Switch Calendar')
22            .margin({ top: 30 })
23            .onClick(() => {
24              this.isLunar = !this.isLunar
25            })
26          DatePicker({
27            start: new Date('1970-1-1'),
28            end: new Date('2100-1-1'),
29            selected: this.selectedDate
30          })
31            .lunar(this.isLunar)
32            .onChange((value: DatePickerResult) => {
33              this.selectedDate.setFullYear(value.year, value.month, value.day)
34              console.info('select current date is: ' + JSON.stringify(value))
35            })
36
37        }.width('100%')
38      }
39    }
40    ```
41
42    ![datePicker](../../../application-dev/reference/arkui-ts/figures/datePicker.gif)
43
442. The data type declaration of the **@State**, **@Provide**, **@Link**, or **@Consume** decorated state variables can consist of only one of the primitive data types or reference data types.
45
46    The **Length**, **ResourceStr**, and **ResourceColor** types are combinations of primitive data types or reference data types. Therefore, they cannot be used by the aforementioned types of state variables.
47    For details about the definitions of **Length**, **ResourceStr**, and **ResourceColor**, see [Types](../../../application-dev/reference/arkui-ts/ts-types.md).
48
49    Example:
50
51    ```ts
52    // xxx.ets
53    @Entry
54    @Component
55    struct IndexPage {
56      // Incorrect: @State message: string | Resource = 'Hello World'
57      @State message: string = 'Hello World'
58      // Incorrect: @State message: ResourceStr = $r('app.string.hello')
59      @State resourceStr: Resource = $r('app.string.hello')
60
61      build() {
62        Row() {
63          Column() {
64            Text(`${this.message}`)
65              .fontSize(50)
66              .fontWeight(FontWeight.Bold)
67          }
68          .width('100%')
69        }
70        .height('100%')
71      }
72    }
73    ```
74
75    ![hello](../../../application-dev/quick-start/figures/hello.PNG)
76
77**Change Impacts**
78
791. If the data type of a state variable decorated by a state decorator is declared as **any**, a build error will occur.
80    ```ts
81    // ArkTS:ERROR Please define an explicit type, not any.
82    @State isLunar: any = false
83    ```
842. If the data type of a state variable decorated by a state decorator is declared as **Date**, a build error will occur.
85    ```ts
86    // ArkTS:ERROR The @State property 'selectedDate' cannot be a 'Date' object.
87    @State selectedDate: Date = new Date('2021-08-08')
88    ```
893. If the data type of a **@State**, **@Provide**, **@Link**, and or **@Consume** decorated state variable is Length, **ResourceStr**, or **ResourceColor**, a build error will occur.
90    ```ts
91    /* ArkTS:ERROR The state variable type here is 'ResourceStr', it contains both a simple type and an object type,
92      which are not allowed to be defined for state variable of a struct.*/
93    @State message: ResourceStr = $r('app.string.hello')
94    ```
95
96**Key API/Component Changes**
97
98N/A
99
100**Adaptation Guide**
101
1021. Explicitly declare the data type for state variables decorated by state decorators.
1032. If a state variable decorated by a state decorator uses the **Date** object, change it to a regular variable – a variable not decorated by any decorator.
1043. Adapt the **@State**, **@Provide**, **@Link**, and **@Consume** decorated variables based on the following code snippet so that they do not use the **Length(string|number|Resource)**, **ResourceStr(string|Resource)**, and **ResourceColor(string|number|Color|Resource)** types:
105
106    ```ts
107    // Incorrect:
108    @State message: ResourceStr = $r('app.string.hello')
109    // Corrected:
110    @State resourceStr: Resource = $r('app.string.hello')
111    ```
112
113## cl.arkui.2 Initialization Rules and Restrictions of Custom Components' Member Variables
114
115Comply with the following rules when using constructors to initialize member variables:
116
117| **From the Variable in the Parent Component (Right) to the Variable in the Child Component (Below)**| **regular** | **@State** | **@Link** | **@Prop** | **@Provide** | **@Consume** | **@ObjectLink** |
118|---------------------------------|----------------------------|------------|-----------|-----------|--------------|--------------|------------------|
119| **regular**                    | Supported                        | Supported        | Supported       | Supported       | Not supported           | Not supported           | Supported              |
120| **@State**                     | Supported                        | Supported        | Supported       | Supported       | Supported          | Supported          | Supported              |
121| **@Link**                      | Not supported                         | Supported (1)     | Supported (1)    | Supported (1)    | Supported (1)       | Supported (1)       | Supported (1)           |
122| **@Prop**                      | Supported                        | Supported        | Supported       | Supported       | Supported          | Supported          | Supported              |
123| **@Provide**                   | Supported                        | Supported        | Supported       | Supported       | Supported          | Supported          | Supported              |
124| **@Consume**                   | Not supported                         | Not supported         | Not supported        | Not supported        | Not supported           | Not supported           | Not supported               |
125| **@ObjectLink**                | Not supported                         | Not supported     | Not supported        | Not supported        | Not supported           | Not supported           | Not supported               |
126
127| **From the Variable in the Parent Component (Right) to the Variable in the Child Component (Below)**| **@StorageLink** | **@StorageProp** | **@LocalStorageLink** | **@LocalStorageProp** |
128|------------------|------------------|------------------|-----------------------|------------------------|
129| **regular**                   | Supported              | Not supported               | Not supported                    | Not supported             |
130| **@State**                    | Supported              | Supported              | Supported                   | Supported                    |
131| **@Link**                     | Supported (1)           | Supported (1)           | Supported (1)                | Supported (1)                 |
132| **@Prop**                     | Supported              | Supported              | Supported                   | Supported                    |
133| **@Provide**                  | Supported              | Supported              | Supported                   | Supported                    |
134| **@Consume**                  | Not supported            | Not supported             | Not supported                 | Not supported                  |
135| **@ObjectLink**               | Not supported            | Not supported             | Not supported                 | Not supported                  |
136
137> **NOTE**
138>
139> **Supported (1)**: The dollar sign ($) must be used, for example, **this.$varA**.
140>
141> **regular**: refers to a regular variable that is not decorated by any decorator.
142
143**@StorageLink**, **@StorageProp**, **@LocalStorageLink**, and **@LocalStorageProp** variables cannot be initialized from the parent component.
144
145**Change Impacts**
146
1471. Variables decorated by **@LocalStorageLink** and **@LocalStorageProp** cannot be initialized from the parent component.
148    ```ts
149    @Entry
150    @Component
151    struct LocalStorageComponent {
152        build() {
153            Column() {
154                Child({
155                  /* ArkTS:ERROR Property 'simpleVarName' in the custom component 'Child' cannot
156                    initialize here (forbidden to specify). */
157                  simpleVarName: 1,
158                  /* ArkTS:ERROR Property 'objectName' in the custom component 'Child' cannot
159                    initialize here (forbidden to specify). */
160                  objectName: new ClassA("x")
161                })
162            }
163        }
164    }
165    @Component
166    struct Child {
167        @LocalStorageLink("storageSimpleProp") simpleVarName: number = 0;
168        @LocalStorageProp("storageObjectProp") objectName: ClassA = new ClassA("x");
169        build() {}
170    }
171    ```
1722. The **@ObjectLink** decorated variable cannot be directly initialized from a decorated variable in the parent component. The source of the parent component must be an array item or object attribute decorated by **@State**, **@Link**, **@Provide**, **@Consume**, or **@ObjectLink**.
173    ```ts
174    let NextID : number = 0;
175
176    @Observed class ClassA {
177      public id : number;
178      public c: number;
179      constructor(c: number) {
180        this.id = NextID++;
181        this.c = c;
182      }
183    }
184
185    @Component
186    struct Child {
187      @ObjectLink varA : ClassA;
188      build() {
189        Row() {
190          Text('ViewA-' + this.varA.id)
191        }
192      }
193    }
194
195    @Component
196    struct Parent {
197      @Link linkValue: ClassA
198      build() {
199        Column() {
200          /* ArkTS:ERROR The @Link property 'linkValue' cannot be assigned to
201            the @ObjectLink property 'varA'.*/
202          Child({ varA: this.linkValue })
203        }
204      }
205    }
206    ```
207
208**Key API/Component Changes**
209
210N/A
211
212**Adaptation Guide**
2131. When building a child component, do not perform the build on the variables decorated by **@LocalStorageLink** and **@LocalStorageProp** in the child component.
214
215   To change these variables from the parent component, use the API provided by the **LocalStorage** (such as the **set** API) to assign values to them.
216
2172. For details about how to use **@ObjectLink**, see [@Observed and @ObjectLink](../../../application-dev/quick-start/arkts-observed-and-objectlink.md).
218