Interfaces vs Types in Typescript
What Is the Difference Between These Statements (Interface vs Type) in Typescript? Interface X { a: Number B: String } Type X = { a: Number B: String }; 6 24...
What is the difference between these statements (interface vs type) in TypeScript?
interface X {
a: number
b: string
}
type X = {
a: number
b: string
};
24 Answers
2019 Update
The current answers and the official documentation are outdated. And for those new to TypeScript, the terminology used isn't clear without examples. Below is a list of up-to-date differences.
Must Read
1. Objects / Functions
Both can be used to describe the shape of an object or a function signature. But the syntax differs.
Interface
interface Point {
x: number;
y: number;
}
interface SetPoint {
(x: number, y: number): void;
}
Type alias
type Point = {
x: number;
y: number;
};
type SetPoint = (x: number, y: number) => void;