• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#  Array literals must contain elements of only inferrable types
2
3Rule ``arkts-no-noninferrable-arr-literals``
4
5**Severity: error**
6
7Basically, ArkTS infers the type of an array literal as a union type of its
8contents. However, a compile-time error occurs if there is at least one
9element with a non-inferrable type (e.g. untyped object literal).
10
11
12## TypeScript
13
14
15```
16
17    let a = [{n: 1, s: "1"}, {n: 2, s : "2"}]
18
19```
20
21## ArkTS
22
23
24```
25
26    class C {
27        n: number = 0
28        s: string = ""
29    }
30
31    let a1 = [{n: 1, s: "1"} as C, {n: 2, s : "2"} as C] // a1 is of type "C[]"
32    let a2: C[] = [{n: 1, s: "1"}, {n: 2, s : "2"}]      // ditto
33
34```
35
36## See also
37
38- Recipe 038:  Object literal must correspond to some explicitly declared class or interface (``arkts-no-untyped-obj-literals``)
39- Recipe 040:  Object literals cannot be used as type declarations (``arkts-no-obj-literals-as-types``)
40
41
42