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
Consider benefits when explicitly typing the return value of a function:
359
-
360
-
- Return values makes it clear and easy to understand to any calling code what type is returned.
361
-
- In the case where there is no return value, the calling code doesn't try to use the undefined value when it shouldn't.
362
-
- Surface potential type errors faster in the future if there are code changes that change the return type of the function.
363
-
- Easier to refactor, since it ensures that the return value is assigned to a variable of the correct type.
364
-
- 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.
365
-
- Although type inference is very convenient, adding return types can save TypeScript compiler a lot of work.
366
-
367
351
### Type any & unknown
368
352
369
353
`any` data type must not be used as it represents literally “any” value that TypeScript defaults to and skips type checking since it cannot infer the type. As such, `any` is dangerous, it can mask severe programming errors.
Consider benefits when explicitly typing the return value of a function:
609
+
610
+
- Return values makes it clear and easy to understand to any calling code what type is returned.
611
+
- In the case where there is no return value, the calling code doesn't try to use the undefined value when it shouldn't.
612
+
- Surface potential type errors faster in the future if there are code changes that change the return type of the function.
613
+
- Easier to refactor, since it ensures that the return value is assigned to a variable of the correct type.
614
+
- 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.
615
+
- Although type inference is very convenient, adding return types can save TypeScript compiler a lot of work.
0 commit comments