• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#  Strict type checking is enforced
2
3Rule ``arkts-strict-typing``
4
5**Severity: error**
6
7Type checker in ArkTS is not optional, the code must be explicitly and
8correctly types to be compiled and run. When porting from the standard TypeScript,
9turn on the following flags: ``noImplicitReturns``, ``strictFunctionTypes``,
10``strictNullChecks``, ``strictPropertyInitialization``.
11
12
13## TypeScript
14
15
16```
17
18    class C {
19        n: number // Compile-time error only with strictPropertyInitialization
20        s: string // Compile-time error only with strictPropertyInitialization
21    }
22
23    // Compile-time error only with noImplicitReturns
24    function foo(s: string): string {
25        if (s != "") {
26            console.log(s)
27            return s
28        } else {
29            console.log(s)
30        }
31    }
32
33    let n: number = null // Compile-time error only with strictNullChecks
34
35```
36
37## ArkTS
38
39
40```
41
42    class C {
43        n: number = 0
44        s: string = ""
45    }
46
47    function foo(s: string): string {
48        console.log(s)
49        return s
50    }
51
52    let n1: number | null = null
53    let n2: number = 0
54
55```
56
57## See also
58
59- Recipe 008:  Use explicit types instead of ``any``, ``unknown`` (``arkts-no-any-unknown``)
60- Recipe 146:  Switching off type checks with in-place comments is not allowed (``arkts-strict-typing-required``)
61
62
63