TypeScript is a statically typed language that builds on JavaScript by adding optional types to the language. Interfaces and types are two essential features in TypeScript that allow developers to define the shape of objects and variables in their code. In this article, we will explore the differences between interfaces and types in TypeScript, and when it is appropriate to use each.
Interfaces
An interface in TypeScript defines the shape of an object. An interface can be used to describe the properties, methods, and events of an object, and is used to enforce consistency between objects. When defining an interface, you can specify the types of properties that an object must have, and the types of arguments that its methods must take.
For example:
interface User {
name: string;
email: string;
age: number;
}
const user: User = {
name: 'John Doe',
email: 'johndoe@example.com',
age: 30
};
In this example, we define an interface called User
that describes the shape of a user object. The User
interface requires that any object that implements it must have properties called name
, email
, and age
, and that they must be of type string
, string
, and number
respectively.
Types
A type in TypeScript is similar to an interface in that it defines the shape of an object or a value. However, there are some important differences between the two. Unlike interfaces, types can be used to define not just the shape of an object, but also the shape of a primitive value, such as a number or a string.
Here's an example of how you might use a type to describe a user ID:
type UserId = string | number;
const userId: UserId = 123;
This allows us to define a single type that can represent multiple possible values.
Differences between Interfaces and Types
Interfaces can extend other interfaces, while types cannot.
Types can describe primitives, while interfaces cannot.
Interfaces can describe objects, while types cannot.
Types can be used to define unions, intersections, and type aliases, while interfaces cannot.
When to use Interfaces
Use interfaces when you want to describe the shape of an object.
Use interfaces when you want to enforce consistency between objects.
Use interfaces when you want to describe the properties, methods, and events of an object.
When to use Types
Use types when you want to describe the shape of a primitive value.
Use types when you want to define unions, intersections, and type aliases.
Use types when you want to describe the shape of a value that can have multiple possible types.
Summary
In general, interfaces are best used when defining the structure of objects. They are particularly useful when you need to enforce a specific shape for objects and make sure that objects conform to that shape. They also provide a way to extend existing interfaces to create new ones, making it easier to build on existing code.
Types, on the other hand, are best used when defining custom types for variables, objects, and other values. They provide a way to create type aliases, union types, and intersection types, making it easier to specify complex type relationships.
Further Readings
https://blog.logrocket.com/types-vs-interfaces-in-typescript/
https://www.typescriptlang.org/play#example/types-vs-interfaces
https://www.geeksforgeeks.org/what-is-the-difference-between-interface-and-type-in-typescript/