mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 15:18:01 +00:00
1.0 KiB
1.0 KiB
Interfaces With The Same Name Are Merged
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 types with the same name, that would result in a type
error.