|
TypeScript Inference defines that TypeScript will automatically detect variables data type, functions return type, Object types, Array Types, etc. This detection is based on the values assigned to the variables and the usage of the code or function calls. What is Inference?
Example 1: Inference of Number and String Types: TypeScript will infer types by looking at the initial values of the variables and functions return values and it will determine the appropriate types for the variables and function return types by checking their usage throughout the code. Javascript
Output: Value of x is 10 Example 2: TypeScript can infer the types of arrays and objects based on their initial values. Javascript
Output :Value of x is Golf Example 3: TypeScript can infer return types also with conditions of the functions based on the values returned from the functions and usage of the functions in the entire code. Javascript
Output: Output of the code. Conclusion: TypeScript’s type inference is a feature that helps developers write simple and clean code by automatically detecting the type of variables and functions return types etc. It also enables writing strongly types code and in figuring out type related errors in initial development phase of the application. |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |