Learn about the core concept of Type
in TypeScript and how it refers to different variables, functions, objects, etc.
Type in TypeScript are the building blocks, which helps us to define the type of value a variable can hold making sure when we try to assign another type of value in the variable, throw and error during compilation saving us from runtime issues in our program.
Type can be assigned to any variable, object and even to arguments and return values of a function, we will look at these one by one.
// syntax of type :<type>
// ':' tells TypeScript compiler that we are defining type
// <type> is used after variable, function or object to define the type of value it will hold.
let counter : number = 1;
// here 'number' means the variable counter will hold only integer values
let countryName : string = 'US';
// here countryName will only hold 'string' values only
Type in TypeScript
Types are inherited from JavaScript and primarily divided into two parts.
- Primitive types also called as built-in types
- User-defined types or custom types
any
– if we do not define type to a variable, object or function, TypeScript will default it to ‘any’ implicitly.- ‘any’ can also be used explicitly if we do not know what values it will hold.
- There are more advanced way of defining ‘Generic’ types. These are covered in later parts of the tutorial.
Primitive Types
Primitive types are inherited from the core JavaScript, where these types were inferred from the value stored in the variable now defined explicitly using specific keywords.
Data Type | Keyword | Description |
---|---|---|
Number | number | In TypeScript number is used to hold integer values e.g. 1, 1.4, etc. |
String | string | Represents text data like ‘Actor’ |
Boolean | boolean | boolean is used to hold true or false |
Void | void | If a function do not return anything we can define it as void type function. |
Null | null | If only one value is permitted null. |
Undefined | undefined | If only one value is permitted undefined. |
Symbol | Symbol | Defined as part of ES2015, symbols can hold unique immutable constant value. |
object | object | Represents instances of user-defined classes, arrays, functions, etc. |
Never | never | Represents values that never occur. |
User-Defined Types
We can combine Primitive types to define type for Functions, Arrays, Objects, Enum and Tuples. This gives flexibility in defining what data the variables will hold, as per the application requirements.
Data Type | Keyword | Description |
---|---|---|
Arrays | Array | Used to defines a variable of array type and what type of values it can hold inside that array. For Example, Array[string] will only hold string values in it. |
Tuples | Tuple | Tuples are like dictionaries and can hold set of key, value pairs in which keys are unique. |
Enum | Enum | Enums define constant values for example name of months in an year |
Array in TypeScript
Let us create an array which can hold names for users in a queue and each name will be of string type. If we try assigning any other type of value lets say a number TypeScript compiler will throw and error.
let usersList : Array = ['John Foster', 'Angelica' ] ;
Tuple in TypeScript
Tuples define set of constant values which a variable can hold and these are fixed values any changes to the values will need updates to tuple definition.
// Defining a tuple to hold two values one is string an other is a number.
let tuple: [string, number] = ["hello", 10];
console.log(tuple);
// Output: ["hello", 10]
Enum in TypeScript
Enum i.e. enumeration is a type of collection which can hold constant values of same type and associated value to it which can be used across the application so that if the values are used is any conditional logic like if..else or switch case we can be sure that the type and value of variable holding that enum will not change.
// Defining a Enum to hold color values which can be used across app.
enum Color {Red, Green, Blue}
let c: Color = Color.Green;
console.log(c); // Output: 1
Summary:
- Every value in typescript has a
type
, if not defined it is inferred asany
. - TypeScript helps us to define custom data types to hold values of same
type
saving us from runtime errors.