TypeScript is a popular programming language that adds static typing to JavaScript. With TypeScript, developers can catch type-related errors early on in the development process, which can lead to more robust and maintainable code. One of the key features of TypeScript is its type system, which allows developers to define types for variables, functions, and other entities. In this tutorial, we will explore the basics of TypeScript types.


What are TypeScript types?

In TypeScript, a type is a classification of a value that describes the kind of data that the value represents. For example, a variable could be of type string, number, boolean, object, null, or undefined. Types help TypeScript check for type-related errors and ensure that code behaves as expected.

Defining variable types

In TypeScript, variables can be declared with a type annotation, which is denoted by a colon followed by the type. For example:

let firstName: string = "John";
let age: number = 30;
let isStudent: boolean = true;

The above code declares three variables, firstName, age, and isStudent, with their respective types of string, number, and boolean. Note that the variable types are specified after the variable name.


Type inference

TypeScript also supports type inference, which allows TypeScript to automatically determine the type of a variable based on its value. For example:

let lastName = "Doe"; // Type inferred as string
let height = 175; // Type inferred as number
let isActive = false; // Type inferred as boolean

In the above code, TypeScript infers the types of the variables lastName, height, and isActive based on their assigned values.


Defining function types

Functions in TypeScript can also be annotated with types for their parameters and return value. For example:

function addNumbers(x: number, y: number): number {
  return x + y;
}

In the above code, the addNumbers function takes two parameters of type number and returns a value of type number.


Defining object types

In TypeScript, object types can be defined using interfaces. An interface is a blueprint that defines the structure of an object. For example:

interface Person {
  firstName: string;
  lastName: string;
  age: number;
}

let person: Person = {
  firstName: "John",
  lastName: "Doe",
  age: 30,
};

In the above code, we define an interface Person that specifies the structure of an object with properties firstName, lastName, and age. We then declare a variable person of type Person and assign it an object with the same structure.


Defining array types

Arrays in TypeScript can also be annotated with types. For example:

let numbers: number[] = [1, 2, 3];
let strings: string[] = ["foo", "bar", "baz"];

In the above code, we declare two variables numbers and strings as arrays of number and string types, respectively.


Union types

In TypeScript, union types allow variables or parameters to accept multiple types. For example:

let age: number | string = 30;
age = "thirty";

In the above code, we declare a variable age that can be of type number or string. We then assign it a value of type number, followed by a value of type string.

Type aliases

TypeScript allows developers to define custom types using type aliases. A type alias is a name given to a type that can be used to refer to it throughout the code. For example:

type Age = number | string;

let age: Age = 30;
age = "thirty";

In the above code, we define a type alias Age that can be of type number or string. We then declare a variable age of type Age and assign it a value of type number, followed by a value of type string.


Type assertions

TypeScript also allows developers to perform type assertions, which is a way of telling the TypeScript compiler that we know more about the type of a value than it does. For example:

let value: any = "hello world";
let length: number = (value as string).length;

In the above code, we declare a variable value of type any, which could be any type. We then use a type assertion to tell TypeScript that value is actually a string, and we assign its length property to a variable length.


Conclusion

In this tutorial, we explored the basics of TypeScript types, including variable types, function types, object types, array types, union types, type aliases, and type assertions. By using TypeScript types, developers can write more reliable and maintainable code, and catch type-related errors early in the development process.