• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#  Some of utility types are not supported
2
3Rule ``arkts-no-utility-types``
4
5**Severity: error**
6
7Currently ArkTS does not support utility types from TypeScript extensions to the
8standard library, except following: ``Partial``, ``Required``, ``Readonly``, ``Record``.
9
10For the type *Record<K, V>*, the type of an indexing expression *rec[index]* is
11of the type *V | undefined*.
12
13
14## TypeScript
15
16
17```
18
19    type Person = {
20        name: string
21        age: number
22        location: string
23    }
24
25    type QuantumPerson = Omit<Person, "location">
26
27    let persons : Record<string, Person> = {
28        "Alice": {
29            name: "Alice",
30            age: 32,
31            location: "Shanghai"
32        },
33        "Bob": {
34            name: "Bob",
35            age: 48,
36            location: "New York"
37        }
38    }
39    console.log(persons["Bob"].age)
40    console.log(persons["Rob"].age) // Runtime exception
41
42```
43
44## ArkTS
45
46
47```
48
49    class Person {
50        name: string = ""
51        age: number = 0
52        location: string = ""
53    }
54
55    class QuantumPerson {
56        name: string = ""
57        age: number = 0
58    }
59
60    type OptionalPerson = Person | undefined
61    let persons : Record<string, OptionalPerson> = {
62    // Or:
63    // let persons : Record<string, Person | undefined> = {
64        "Alice": {
65            name: "Alice",
66            age: 32,
67            location: "Shanghai"
68        },
69        "Bob": {
70            name: "Bob",
71            age: 48,
72            location: "New York"
73        }
74    }
75    console.log(persons["Bob"]!.age)
76    if (persons["Rob"]) { // Explicit value check, no runtime exception
77        console.log(persons["Rob"].age)
78    }
79
80```
81
82
83