Here is the declartion of an interface in TypeScript.
interface Person {
name: string
}
What if I were to add a separate interface declaration with the same name,
Person
?
interface Person {
age: number
}
TypeScript performs declaration merging. So the types of the two interfaces
would be combined. So, a variable of type Person
can have an name
and an
age
.
const person: Person = {
age: 22,
name: 'Bob'
}
See a live example in the TS Playground.
This is different from how object type declarations handle it. If I were to try
to define two separate type
s with the same name, that would result in a type
error.