180

On line 60359 of this type definition file, there is the following declaration:

type ActivatedEventHandler = (
    ev: Windows.ApplicationModel.Activation.IActivatedEventArgs 
        & WinRTEvent<any>
) => void;

What does the & sigil mean in this context?

ruffin
  • 16,507
  • 9
  • 88
  • 138
Carl Patenaude Poulin
  • 6,238
  • 5
  • 24
  • 46
  • 5
    Intersection types: http://www.typescriptlang.org/docs/handbook/advanced-types.html#intersection-types – Nitzan Tomer Jul 11 '16 at 23:24
  • The newer link to Intersection Types: https://www.typescriptlang.org/docs/handbook/2/objects.html#intersection-types – aderchox Jun 20 '23 at 18:47

2 Answers2

172

& in a type position means intersection type.

More from typescript docs on Intersection Types:

https://www.typescriptlang.org/docs/handbook/2/objects.html#intersection-types

Citation from the docs linked above:

Intersection types are closely related to union types, but they are used very differently. An intersection type combines multiple types into one. This allows you to add together existing types to get a single type that has all the features you need. For example, Person & Serializable & Loggable is a type which is all of Person and Serializable and Loggable. That means an object of this type will have all members of all three types.

For example, if you had networking requests with consistent error handling then you could separate out the error handling into it’s own type which is merged with types which correspond to a single response type.

interface ErrorHandling {
  success: boolean;
  error?: { message: string };
}

interface ArtworksData {
  artworks: { title: string }[];
}

interface ArtistsData {
  artists: { name: string }[];
}

// These interfaces are composed to have
// consistent error handling, and their own data.

type ArtworksResponse = ArtworksData & ErrorHandling;
type ArtistsResponse = ArtistsData & ErrorHandling;

const handleArtistsResponse = (response: ArtistsResponse) => {
  if (response.error) {
    console.error(response.error.message);
    return;
  }

  console.log(response.artists);
};
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
basarat
  • 261,912
  • 58
  • 460
  • 511
50

Intersection type in Typescript

  • A & in TS in the context of a types means an intersection type.
  • It merges all properties of 2 object types together and creates a new type

Example:

type dog = {age: number, woof: Function};
type cat = {age: number, meow: Function};

// Type weird is an intersection of cat and dog
// it needs to have all properties of them combined
type weird = dog & cat;

const weirdAnimal: weird = {age: 2, woof: () => {'woof'}, meow: () => {'meow'}}

interface extaprop {
    color: string
}

type catDog = weird & extaprop; // type now also has added color
const weirdAnimal2: catDog = {age: 2, woof: () => {'woof'}, meow: () => {'meow'}, color: 'red'}


// This is different form a union type
// The type below means either a cat OR a dog
type dogOrCat = dog | cat;
Willem van der Veen
  • 33,665
  • 16
  • 190
  • 155
  • 10
    Isn't this technically a `union` and not an `intersection`? – ecoe Jul 26 '22 at 19:15
  • 2
    "Intersection" refers to the resulting _type_, not the operation performed on the properties. An object belonging to both Type A and Type B must have all properties in A (so that it is an instance of A) while also having all the properties of B (so that it is also an instance of B). In other words, an intersection of the types must have the union of each type's properties. Similarly, the union of types will have the intersection of those types' properties. – Rax Adaam Nov 15 '22 at 20:29
  • this seems similar to `extends` right? – Embedded_Mugs Jan 18 '23 at 19:43