Mon Dec 27 2021
Various kinds of data types in TypeScript and their utility
TypeScript is an open-source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript and adds optional static typing to the language. It is designed for development of large applications and trans compiles to JavaScript. As TypeScript is a superset of JavaScript, existing JavaScript programs are also valid TypeScript programs. TypeScript may be used to develop JavaScript applications for both client-side and server-side (Node.js) execution. This language supports definition files that can contain type information of existing JavaScript libraries, much like C++ header files can describe the structure of existing object files. The typescript was first made public in October 2012 (at version 0.8), after two years of internal development at Microsoft. Anders Hejlsberg, the lead architect of C# and creator of Delphi and Turbo Pascal, has worked on the development of TypeScript. The TypeScript compiler is itself written in TypeScript and compiled to JavaScript. It is licensed under the Apache 2 License.
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.
Boolean (boolean) - The most basic data type is the simple true/false value, which JavaScript and TypeScript call a boolean value.
String (string) - It is used to represent a sequence of characters. Just like JavaScript, TypeScript also uses double quotes (") or single quotes (') to surround string data.
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.
Array (arrays) - TypeScript, like JavaScript, allows you to work with arrays of values. Array types can be written in one of two ways. In the first, you use the type of the elements followed by '[ ]' to denote an array of that element type. The second way uses a generic array type, 'Array<elemType>'.
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.
Enumerations (enums) - A helpful addition to the standard set of data types from JavaScript is the enum. As in languages like C#, an enum is a way of giving more friendly names to sets of numeric values.
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!