• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#  Function return type inference is limited
2
3Rule ``arkts-no-implicit-return-types``
4
5**Severity: error**
6
7ArkTS supports type inference for function return types, but this functionality
8is currently restricted. In particular, when the expression in the ``return``
9statement is a call to a function or method whose return value type is omitted,
10a compile-time error occurs. In case of any such error, specify the return type
11explicitly.
12
13
14## TypeScript
15
16
17```
18
19    // Compile-time error with noImplicitAny
20    function f(x: number) {
21        if (x <= 0) {
22            return x
23        }
24        return g(x)
25    }
26
27    // Compile-time error with noImplicitAny
28    function g(x: number) {
29        return f(x - 1)
30    }
31
32    function doOperation(x: number, y: number) {
33        return x + y
34    }
35
36    console.log(f(10))
37    console.log(doOperation(2, 3))
38
39```
40
41## ArkTS
42
43
44```
45
46    // Explicit return type is required:
47    function f(x: number) : number {
48        if (x <= 0) {
49            return x
50        }
51        return g(x)
52    }
53
54    // Return type may be omitted, it is inferred from f's explicit type:
55    function g(x: number) {
56        return f(x - 1)
57    }
58
59    // In this case, return type will be inferred
60    function doOperation(x: number, y: number) {
61        return x + y
62    }
63
64    console.log(f(10))
65    console.log(doOperation(2, 3))
66
67```
68
69
70