13af6ab5fSopenharmony_ci#  Object literal must correspond to some explicitly declared class or interface
23af6ab5fSopenharmony_ci
33af6ab5fSopenharmony_ciRule ``arkts-no-untyped-obj-literals``
43af6ab5fSopenharmony_ci
53af6ab5fSopenharmony_ci**Severity: error**
63af6ab5fSopenharmony_ci
73af6ab5fSopenharmony_ciArkTS supports usage of object literals if the compiler can infer to what
83af6ab5fSopenharmony_ciclasses or interfaces such literals correspond to. A compile-time error
93af6ab5fSopenharmony_cioccurs otherwise. Using literals to initialize classes and interfaces is
103af6ab5fSopenharmony_cispecifically not supported in the following contexts:
113af6ab5fSopenharmony_ci
123af6ab5fSopenharmony_ci* Initialization of anything that has ``any``, ``Object``, or ``object`` type
133af6ab5fSopenharmony_ci* Initialization of classes or interfaces with methods
143af6ab5fSopenharmony_ci* Initialization of classes which declare a ``constructor`` with parameters
153af6ab5fSopenharmony_ci* Initialization of classes with ``readonly`` fields
163af6ab5fSopenharmony_ci
173af6ab5fSopenharmony_ci
183af6ab5fSopenharmony_ci## TypeScript
193af6ab5fSopenharmony_ci
203af6ab5fSopenharmony_ci
213af6ab5fSopenharmony_ci```
223af6ab5fSopenharmony_ci
233af6ab5fSopenharmony_ci    let o1 = {n: 42, s: "foo"}
243af6ab5fSopenharmony_ci    let o2: Object = {n: 42, s: "foo"}
253af6ab5fSopenharmony_ci    let o3: object = {n: 42, s: "foo"}
263af6ab5fSopenharmony_ci
273af6ab5fSopenharmony_ci    let oo: Object[] = [{n: 1, s: "1"}, {n: 2, s: "2"}]
283af6ab5fSopenharmony_ci
293af6ab5fSopenharmony_ci    class C2 {
303af6ab5fSopenharmony_ci        s: string
313af6ab5fSopenharmony_ci        constructor(s: string) {
323af6ab5fSopenharmony_ci            this.s = "s =" + s
333af6ab5fSopenharmony_ci        }
343af6ab5fSopenharmony_ci    }
353af6ab5fSopenharmony_ci    let o4: C2 = {s: "foo"}
363af6ab5fSopenharmony_ci
373af6ab5fSopenharmony_ci    class C3 {
383af6ab5fSopenharmony_ci        readonly n: number = 0
393af6ab5fSopenharmony_ci        readonly s: string = ""
403af6ab5fSopenharmony_ci    }
413af6ab5fSopenharmony_ci    let o5: C3 = {n: 42, s: "foo"}
423af6ab5fSopenharmony_ci
433af6ab5fSopenharmony_ci    abstract class A {}
443af6ab5fSopenharmony_ci    let o6: A = {}
453af6ab5fSopenharmony_ci
463af6ab5fSopenharmony_ci    class C4 {
473af6ab5fSopenharmony_ci        n: number = 0
483af6ab5fSopenharmony_ci        s: string = ""
493af6ab5fSopenharmony_ci        f() {
503af6ab5fSopenharmony_ci            console.log("Hello")
513af6ab5fSopenharmony_ci        }
523af6ab5fSopenharmony_ci    }
533af6ab5fSopenharmony_ci    let o7: C4 = {n: 42, s: "foo", f : () => {}}
543af6ab5fSopenharmony_ci
553af6ab5fSopenharmony_ci    class Point {
563af6ab5fSopenharmony_ci        x: number = 0
573af6ab5fSopenharmony_ci        y: number = 0
583af6ab5fSopenharmony_ci    }
593af6ab5fSopenharmony_ci
603af6ab5fSopenharmony_ci    function id_x_y(o: Point): Point {
613af6ab5fSopenharmony_ci        return o
623af6ab5fSopenharmony_ci    }
633af6ab5fSopenharmony_ci
643af6ab5fSopenharmony_ci    // Structural typing is used to deduce that p is Point:
653af6ab5fSopenharmony_ci    let p = {x: 5, y: 10}
663af6ab5fSopenharmony_ci    id_x_y(p)
673af6ab5fSopenharmony_ci
683af6ab5fSopenharmony_ci    // A literal can be contextually (i.e., implicitly) typed as Point:
693af6ab5fSopenharmony_ci    id_x_y({x: 5, y: 10})
703af6ab5fSopenharmony_ci
713af6ab5fSopenharmony_ci```
723af6ab5fSopenharmony_ci
733af6ab5fSopenharmony_ci## ArkTS
743af6ab5fSopenharmony_ci
753af6ab5fSopenharmony_ci
763af6ab5fSopenharmony_ci```
773af6ab5fSopenharmony_ci
783af6ab5fSopenharmony_ci    class C1 {
793af6ab5fSopenharmony_ci        n: number = 0
803af6ab5fSopenharmony_ci        s: string = ""
813af6ab5fSopenharmony_ci    }
823af6ab5fSopenharmony_ci
833af6ab5fSopenharmony_ci    let o1: C1 = {n: 42, s: "foo"}
843af6ab5fSopenharmony_ci    let o2: C1 = {n: 42, s: "foo"}
853af6ab5fSopenharmony_ci    let o3: C1 = {n: 42, s: "foo"}
863af6ab5fSopenharmony_ci
873af6ab5fSopenharmony_ci    let oo: C1[] = [{n: 1, s: "1"}, {n: 2, s: "2"}]
883af6ab5fSopenharmony_ci
893af6ab5fSopenharmony_ci    class C2 {
903af6ab5fSopenharmony_ci        s: string
913af6ab5fSopenharmony_ci        constructor(s: string) {
923af6ab5fSopenharmony_ci            this.s = "s =" + s
933af6ab5fSopenharmony_ci        }
943af6ab5fSopenharmony_ci    }
953af6ab5fSopenharmony_ci    let o4 = new C2("foo")
963af6ab5fSopenharmony_ci
973af6ab5fSopenharmony_ci    class C3 {
983af6ab5fSopenharmony_ci        n: number = 0
993af6ab5fSopenharmony_ci        s: string = ""
1003af6ab5fSopenharmony_ci    }
1013af6ab5fSopenharmony_ci    let o5: C3 = {n: 42, s: "foo"}
1023af6ab5fSopenharmony_ci
1033af6ab5fSopenharmony_ci    abstract class A {}
1043af6ab5fSopenharmony_ci    class C extends A {}
1053af6ab5fSopenharmony_ci    let o6: C = {} // or let o6: C = new C()
1063af6ab5fSopenharmony_ci
1073af6ab5fSopenharmony_ci    class C4 {
1083af6ab5fSopenharmony_ci        n: number = 0
1093af6ab5fSopenharmony_ci        s: string = ""
1103af6ab5fSopenharmony_ci        f() {
1113af6ab5fSopenharmony_ci            console.log("Hello")
1123af6ab5fSopenharmony_ci        }
1133af6ab5fSopenharmony_ci    }
1143af6ab5fSopenharmony_ci    let o7 = new C4()
1153af6ab5fSopenharmony_ci    o7.n = 42
1163af6ab5fSopenharmony_ci    o7.s = "foo"
1173af6ab5fSopenharmony_ci
1183af6ab5fSopenharmony_ci    class Point {
1193af6ab5fSopenharmony_ci        x: number = 0
1203af6ab5fSopenharmony_ci        y: number = 0
1213af6ab5fSopenharmony_ci
1223af6ab5fSopenharmony_ci        // constructor() is used before literal initialization
1233af6ab5fSopenharmony_ci        // to create a valid object. Since there is no other Point constructors,
1243af6ab5fSopenharmony_ci        // constructor() is automatically added by compiler
1253af6ab5fSopenharmony_ci    }
1263af6ab5fSopenharmony_ci
1273af6ab5fSopenharmony_ci    function id_x_y(o: Point): Point {
1283af6ab5fSopenharmony_ci        return o
1293af6ab5fSopenharmony_ci    }
1303af6ab5fSopenharmony_ci
1313af6ab5fSopenharmony_ci    // Explicit type is required for literal initialization
1323af6ab5fSopenharmony_ci    let p: Point = {x: 5, y: 10}
1333af6ab5fSopenharmony_ci    id_x_y(p)
1343af6ab5fSopenharmony_ci
1353af6ab5fSopenharmony_ci    // id_x_y expects Point explicitly
1363af6ab5fSopenharmony_ci    // New instance of Point is initialized with the literal
1373af6ab5fSopenharmony_ci    id_x_y({x: 5, y: 10})
1383af6ab5fSopenharmony_ci
1393af6ab5fSopenharmony_ci```
1403af6ab5fSopenharmony_ci
1413af6ab5fSopenharmony_ci## See also
1423af6ab5fSopenharmony_ci
1433af6ab5fSopenharmony_ci- Recipe 040:  Object literals cannot be used as type declarations (``arkts-no-obj-literals-as-types``)
1443af6ab5fSopenharmony_ci- Recipe 043:  Array literals must contain elements of only inferrable types (``arkts-no-noninferrable-arr-literals``)
1453af6ab5fSopenharmony_ci
1463af6ab5fSopenharmony_ci
147