#Do declaration merged interfaces behave differently from intersected (&) interfaces?
12 messages · Page 1 of 1 (latest)
Preview:```ts
interface Merged {
foo: string
}
interface Merged {
foo: string
bar: string
}
declare const m: Merged
m.bar
m.foo
interface A {
foo: string
}
interface B {
foo: string
bar: string
}
declare const c: A & B
c.bar
c.foo```
now try changing the type of foo in the second declaration
oh that's strange. The first example is a compilation error, the second is a never type
Is that the only difference?
the hover text is different too, not that that really makes much of a difference
fwiw if hover text matters there are other options:
interface Child extends Parent {} // well this has the same hover text as Merged
type Expand<T> = T extends T ? { [K in keyof T]: T[K] } : never;
on an unrelated note
generally you shouldn't be using declaration merging for types you control
since you can simply add to the original declaration
Okay that's useful information. I didn't glean that from reading the handbook. In fact, I thought there could be some way I could leverage this for internal encapsulation (I have no idea how, but it was one of the questions in my mind as I learned the concept)
Thank you both!