#Do declaration merged interfaces behave differently from intersected (&) interfaces?

12 messages · Page 1 of 1 (latest)

fluid surge
#

This example seems to give the same result. Is there some nuance I'm not aware of?

dull baneBOT
#
Bawdy Ink Slinger#9429

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```

noble oriole
#

now try changing the type of foo in the second declaration

fluid surge
#

Is that the only difference?

undone sonnet
#

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

fluid surge
#

Thank you both!