Skip to content

Latest commit

 

History

History
39 lines (30 loc) · 1.02 KB

interfaces-with-the-same-name-are-merged.md

File metadata and controls

39 lines (30 loc) · 1.02 KB

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.

source