Consider the types FooBar1 and FooBar2 defined as follows:
type Foo = { foo: string };
type Bar = { bar: number };
type FooBar1 = Foo & Bar;
type FooBar2 = { foo: string; bar: number };
Question: What is the difference between FooBar1 and FooBar2?
My attempt / research:
- They are bidirectionally assignable to each other!
(checked manually and with tsd- see here)
- Still, they are not identical to each other! (checked with tsd- see here)
- VSCode's intellisense does not collapse { foo } & { bar }automatically into{ foo, bar }, while it does collapse other complex types to simpler forms, such asNonNullable<string | undefined>tostring:
// |------------------------------------------------------------|
// | let x: {                                                   |
// |     a: string;                                             |
// | } & {                                                      |
// |     b: string;                                             |
// | }                                                          |
// | -----------------------------------------------------------|
//  ^
//  | When hovering `x` here:
//  |
let x: { a: string } & { b: string };
Edit: Difference between extending and intersecting interfaces in TypeScript? has been suggested as a duplicate but I disagree. Instead of comparing an intersection to the extension of an interface, I am comparing an intersection to another raw type, no interfaces or extensions involved.
 
    