You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Including return type annotations is highly encouraged, although not required ([eslint rule](https://typescript-eslint.io/rules/explicit-function-return-type/)).
141
-
142
-
Consider benefits when explicitly typing the return value of a function:
143
-
144
-
- Return values makes it clear and easy to understand to any calling code what type is returned.
145
-
- In the case where there is no return value, the calling code doesn't try to use the undefined value when it shouldn't.
146
-
- Surface potential type errors faster in the future if there are code changes that change the return type of the function.
147
-
- Easier to refactor, since it ensures that the return value is assigned to a variable of the correct type.
148
-
- Similar to writing tests before implementation (TDD), defining function arguments and return type, gives you the opportunity to discuss the feature functionality and its interface ahead of implementation.
149
-
- Although type inference is very convenient, adding return types can save TypeScript compiler a lot of work.
150
-
151
138
### Discriminated union
152
139
153
140
If there's only one TypeScript feature to choose from, embrace discriminated unions.
- Discriminated unions make refactoring and maintenance easier by providing a centralized definition of related types. When adding or modifying types within the union, the compiler reports any inconsistencies throughout the codebase.
187
174
- IDEs can leverage discriminated unions to provide better autocompletion and type inference.
188
175
176
+
### Return Types
177
+
178
+
Including return type annotations is highly encouraged, although not required ([eslint rule](https://typescript-eslint.io/rules/explicit-function-return-type/)).
179
+
180
+
Consider benefits when explicitly typing the return value of a function:
181
+
182
+
- Return values makes it clear and easy to understand to any calling code what type is returned.
183
+
- In the case where there is no return value, the calling code doesn't try to use the undefined value when it shouldn't.
184
+
- Surface potential type errors faster in the future if there are code changes that change the return type of the function.
185
+
- Easier to refactor, since it ensures that the return value is assigned to a variable of the correct type.
186
+
- Similar to writing tests before implementation (TDD), defining function arguments and return type, gives you the opportunity to discuss the feature functionality and its interface ahead of implementation.
187
+
- Although type inference is very convenient, adding return types can save TypeScript compiler a lot of work.
188
+
189
189
### Template Literal Types
190
190
191
191
Embrace using template literal types, instead of just (wide) `string` type.
0 commit comments