Dec 27, 2021
Various kinds of data types in TypeScript and their utility
In TypeScript, type System represents different types of data types which are supported by TypeScript.
The data type classification is as given below:
Number (number) - It is used to represent both Integer as well as Floating-Point numbers.
Void (void) - Generally, It is used on function return-types.
Null (null) - It is used when an object does not have any value.
Undefined (undefined) - Denotes value given to uninitialized variable.
Any (any) - In built-in data types, 'any' is a special data-type, also the super data-type of all data types. If a variable is declared with any data type then we can assign any type of value to that variable.
Never - The never type represents the type of values that never occur. For instance, never is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns; Variables also acquire the type never when narrowed by any type guards that can never be true. The never type is a subtype of, and assignable to, every type; however, no type is a subtype of, or assignable to, never (except never itself). Even any isn’t assignable to never.
Object (object) - It is a type that represents the non-primitive type, i.e. anything that is not number, string, boolean, symbol, null, or undefined. With object type, APIs like Object.create can be better represented.
Apart from built-in data types, a user can also define its own data type. User-defined types include Enumerations (enums), classes, interfaces, arrays, and tuple.
Tuple (tuple) - Tuple types allow you to express an array where the type of a fixed number of elements is known, but need not be the same.
Interfaces - As in other programming languages, "Interfaces are contracts to enforce certain rules". The same principle of Interface applies in the TypeScript as well however TypeScript is not as strict as other programming languages interfaces are. TypeScript type-checking only checks for the shape of the interfaces. This type of checking is also called duck typing or structural subtyping.
Now, you get the data types that are available in TypeScript. It can help you avoid a lot of errors when writing large programs.
If you have any suggestions related to this post, let us know in the comments. Thank you!