• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#  Object literal must correspond to some explicitly declared class or interface
2
3Rule ``arkts-no-untyped-obj-literals``
4
5**Severity: error**
6
7ArkTS supports usage of object literals if the compiler can infer to what
8classes or interfaces such literals correspond to. A compile-time error
9occurs otherwise. Using literals to initialize classes and interfaces is
10specifically not supported in the following contexts:
11
12* Initialization of anything that has ``any``, ``Object``, or ``object`` type
13* Initialization of classes or interfaces with methods
14* Initialization of classes which declare a ``constructor`` with parameters
15* Initialization of classes with ``readonly`` fields
16
17
18## TypeScript
19
20
21```
22
23    let o1 = {n: 42, s: "foo"}
24    let o2: Object = {n: 42, s: "foo"}
25    let o3: object = {n: 42, s: "foo"}
26
27    let oo: Object[] = [{n: 1, s: "1"}, {n: 2, s: "2"}]
28
29    class C2 {
30        s: string
31        constructor(s: string) {
32            this.s = "s =" + s
33        }
34    }
35    let o4: C2 = {s: "foo"}
36
37    class C3 {
38        readonly n: number = 0
39        readonly s: string = ""
40    }
41    let o5: C3 = {n: 42, s: "foo"}
42
43    abstract class A {}
44    let o6: A = {}
45
46    class C4 {
47        n: number = 0
48        s: string = ""
49        f() {
50            console.log("Hello")
51        }
52    }
53    let o7: C4 = {n: 42, s: "foo", f : () => {}}
54
55    class Point {
56        x: number = 0
57        y: number = 0
58    }
59
60    function id_x_y(o: Point): Point {
61        return o
62    }
63
64    // Structural typing is used to deduce that p is Point:
65    let p = {x: 5, y: 10}
66    id_x_y(p)
67
68    // A literal can be contextually (i.e., implicitly) typed as Point:
69    id_x_y({x: 5, y: 10})
70
71```
72
73## ArkTS
74
75
76```
77
78    class C1 {
79        n: number = 0
80        s: string = ""
81    }
82
83    let o1: C1 = {n: 42, s: "foo"}
84    let o2: C1 = {n: 42, s: "foo"}
85    let o3: C1 = {n: 42, s: "foo"}
86
87    let oo: C1[] = [{n: 1, s: "1"}, {n: 2, s: "2"}]
88
89    class C2 {
90        s: string
91        constructor(s: string) {
92            this.s = "s =" + s
93        }
94    }
95    let o4 = new C2("foo")
96
97    class C3 {
98        n: number = 0
99        s: string = ""
100    }
101    let o5: C3 = {n: 42, s: "foo"}
102
103    abstract class A {}
104    class C extends A {}
105    let o6: C = {} // or let o6: C = new C()
106
107    class C4 {
108        n: number = 0
109        s: string = ""
110        f() {
111            console.log("Hello")
112        }
113    }
114    let o7 = new C4()
115    o7.n = 42
116    o7.s = "foo"
117
118    class Point {
119        x: number = 0
120        y: number = 0
121
122        // constructor() is used before literal initialization
123        // to create a valid object. Since there is no other Point constructors,
124        // constructor() is automatically added by compiler
125    }
126
127    function id_x_y(o: Point): Point {
128        return o
129    }
130
131    // Explicit type is required for literal initialization
132    let p: Point = {x: 5, y: 10}
133    id_x_y(p)
134
135    // id_x_y expects Point explicitly
136    // New instance of Point is initialized with the literal
137    id_x_y({x: 5, y: 10})
138
139```
140
141## See also
142
143- Recipe 040:  Object literals cannot be used as type declarations (``arkts-no-obj-literals-as-types``)
144- Recipe 043:  Array literals must contain elements of only inferrable types (``arkts-no-noninferrable-arr-literals``)
145
146
147