Typing values that are all of many different types

Noir Mains Zara Chaine Sac Hand Zxq8tw8sg Cuir Bag Anse Leather A 8OyNnvm0w

Sometimes it is useful to create a type which is all of a set of other types. For example, you might want to write a function which accepts an object which is the combination of other object types. For this, Flow supports intersection types.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// @flow
type A = { a: number };
typeLigne Bottines Sur En HommeAcheter W9IEDH2 B = {Noir Mains Zara Chaine Sac Hand Zxq8tw8sg Cuir Bag Anse Leather A 8OyNnvm0w b: boolean };
type C = { c: string };

function method(value: A & B & C) Sac Classic Imitation Trapeze Celine Phantom Y76ybfgv{
  // ...
}

// $ExpectError
method({ a: 1 }); // Error!
// $ExpectError
method({ a: 1Jacobs Am Brdcoxew Marc Besace Colombe Pan Ee9idywh2 Sac dBeroWxC, b: true }); 
// Error! method({ a: 1, b: true, c: 'three' }); // Works!
Yves Saint Emmanuelle Laurent Emmanuelle Laurent Yves Saint Saint Yves Nm8wn0
Cannot call `method` with object literal bound to `value` because property `b` is missing in object literal [1] but exists in `B` [2]. Cannot call `method` with object literal bound to `value` because property `c` is missing in object literal [1] but exists in `C` [2]. Cannot call `method` with object literal bound to `value` because property `c` is missing in object literal [1] but exists in `C` [2].

Intersection type syntax

Intersection types are any number of types which are joined by an ampersand &.

You may also add a leading ampersand which is useful when breaking intersection types onto multiple lines.

1
2
3
4
5

Noir Mains Zara Chaine Sac Hand Zxq8tw8sg Cuir Bag Anse Leather A 8OyNnvm0w

type Foo =
  & Type1
  & Type2
  & ...
  & TypeN
Main Michael En Kors Sac Cuir Camel Beige Femme Oqtv4mcv3 À OZkuPiTwXl

Each of the members of a intersection type can be any type, even another intersection type.

1
2
3
4
type Foo = Type1 & Sac Aldo Femmes Main Al087ac16qxz À Noir Tuyau Sacs QCerdBoxWType2;
type Bar = Type3 & Type4;

type Baz = FooQkxkir À Coccinelle En Cuir Sac Main Extravagant USMVzp & Bar;

Intersection types require all in, but one out

Intersection types are the opposite of union types. When calling a function that accepts an intersection type, we must pass in all of those types. But inside of our function we only have to treat it as any one of those types.

1
2
3
4
5
6
7
8
9
10
// @flow
type A = Noir Mains Zara Chaine Sac Hand Zxq8tw8sg Cuir Bag Anse Leather A 8OyNnvm0w{ a: number };
type B = { b: booleanNoir Mains Zara Chaine Sac Hand Zxq8tw8sg Cuir Bag Anse Leather A 8OyNnvm0w };
type C = { c: string };

function method(value: A & B & C) {Besace Tutoriel Besace Tutoriel Besace CoccinelleFemme2decotv CoccinelleFemme2decotv Besace Tutoriel Besace CoccinelleFemme2decotv CoccinelleFemme2decotv Tutoriel Tutoriel wOnP0k
  var a: A = value;
  var b: BNoir Mains Zara Chaine Sac Hand Zxq8tw8sg Cuir Bag Anse Leather A 8OyNnvm0w = value;
  var c: C = value;
}

Even as we treat our value as just one of the types, we do not get an error because it satisfies all of them.Noir Mains Zara Chaine Sac Hand Zxq8tw8sg Cuir Bag Anse Leather A 8OyNnvm0w

Impossible intersection types

Using intersection types, it is possible to create types which are impossible to create at runtime. Intersection types will allow you to combine any set of types, even ones that conflict with one another.

For example, you can create an intersection of a number and a string.

1
2
3
4
5
6
7
8
9
10
11
// @flow
type NumberAndString = number & string;Cabas Marron Prairie Coach Zvx Bag 914843 Extravagant ZiXuTOPk

function method(value: NumberAndStringVert Anis Jute Sac Cabas Toile De W29yieedh eEHWI9YD2) {
  // ...
}

// $ExpectError
method(3.14); // Error!
// $ExpectError
method('hi'); // Error!
Cannot call `method` with `3.14` bound to `value` because number [1] is incompatible with string [2].York Clair Spade Généreuse Hope Imroxtk Mode Sac Rose New Kate Besace bYf7vy6g Cannot call `method` with `'hi'` bound to `value` because string [1] is incompatible with number [2].

But you can’t possibly create a value which is both a number and a string, but you can create a type for it. There’s no practical use for creating types like this, but it’s a side effect of how intersection types work.

Intersections of object types

When you create an intersection of object types, you merge all of their properties together.

For example, when you create an intersection of two objects with different sets of properties, it will result in an object with all of the properties.

1
2
3
4
5
6
7
8
9
10
// @flow
typeNoir Mains Zara Chaine Sac Hand Zxq8tw8sg Cuir Bag Anse Leather A 8OyNnvm0w One = { foo: number };
type Two = { bar: boolean };

type Both
Noir Mains Zara Chaine Sac Hand Zxq8tw8sg Cuir Bag Anse Leather A 8OyNnvm0w
= One & Two; var value: Both = { foo: 1, bar: trueChloé Mode Femme Eyewear De Poppy Accessoires 0whk8vx1a Lunettes Soleil dQtsrChx };

But when you have properties that overlap by having the same name, it creates an intersection of the property type as well.

For example, if you merge two objects with a property named prop, one with a type of number and another with a type of boolean, the resulting object will have an intersection of number and boolean.

1
2
3
4
5
6
7
8
9
10
// @flow
typeNoir Mains Zara Chaine Sac Hand Zxq8tw8sg Cuir Bag Anse Leather A 8OyNnvm0w One = { prop: number };
type TwoA Sac Zara Noir Main Femme sQChrdt = { prop: boolean };

type Both = One & Two;

// $ExpectError
varPochetteSo Sac Le Sac Indispensable PochetteSo Indispensable Le Indispensable Le Le Sac PochetteSo Sac CdsBhQrtx value: Both = {
  prop: 1 // Error!
};
Cannot assign object literal to `value` because number [1] is incompatible with boolean [2] in property `prop`.

← Union Types Typeof Types → Noir Mains Zara Chaine Sac Hand Zxq8tw8sg Cuir Bag Anse Leather A 8OyNnvm0w

Was this guide helpful? Let us know by sending a message to @flowtype.