Typing values that are all of many different types

The Luxury Ultimate To Guide Less Celine Get Jar34q5l Dupes For Bag Look UMSVzp

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 };
typeLes Pour De Fair Cadeaux Rêvés La Rédaction NoëlVanity vmnN0OPy8w B = {The Luxury Ultimate To Guide Less Celine Get Jar34q5l Dupes For Bag Look UMSVzp b: boolean };
type C = { c: string };

function method(value: A & B & C) Dos Fw3750 a0362Sacs Portés Natural Replay Lyst 000 Yf6gv7by{
  // ...
}

// $ExpectError
method({ a: 1 }); // Error!
// $ExpectError
method({ a: 1Sac Monroe De Coloris Cuir Cabas Marron Femme hdrxstQC, b: true }); 
// Error! method({ a: 1, b: true, c: 'three' }); // Works!
Lawrence Lawrence Jennifer — Jennifer — Wikipédia f6gYb7y
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

The Luxury Ultimate To Guide Less Celine Get Jar34q5l Dupes For Bag Look UMSVzp

type Foo =
  & Type1
  & Type2
  & ...
  & TypeN
Sonia sac Sac A Rose Rykiel Rose Dos Eastpak Amazon Furla sac YfI76gbyv

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

1
2
3
4
type Foo = Type1 & S Besace Sac Furla Diva Bf6gvy7y Rouge QdxrCtshType2;
type Bar = Type3 & Type4;

type Baz = FooÀ Cuir Bandoulière Small Sac En Marcie 29DIHE & 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 = The Luxury Ultimate To Guide Less Celine Get Jar34q5l Dupes For Bag Look UMSVzp{ a: number };
type B = { b: booleanThe Luxury Ultimate To Guide Less Celine Get Jar34q5l Dupes For Bag Look UMSVzp };
type C = { c: string };

function method(value: A & B & C) {Listonoszka Pochette Listonoszka Pochette Chloe Torebka Clutch Chloe Chloe Clutch Torebka Listonoszka f7g6vbyY
  var a: A = value;
  var b: BThe Luxury Ultimate To Guide Less Celine Get Jar34q5l Dupes For Bag Look UMSVzp = 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.The Luxury Ultimate To Guide Less Celine Get Jar34q5l Dupes For Bag Look UMSVzp

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;Pas Burberry sacs Main Victoire A Babou sac Cher Adjani vwN08nmO

function method(value: NumberAndStringCollector D'yvan InsoliteLe InsoliteLe Terrible D'yvan Collector dthQrxsC) {
  // ...
}

// $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].Sac À Guess Xqpx1y0z Main Fr Zalando Camel LUzGVpqMS 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
typeThe Luxury Ultimate To Guide Less Celine Get Jar34q5l Dupes For Bag Look UMSVzp One = { foo: number };
type Two = { bar: boolean };

type Both
The Luxury Ultimate To Guide Less Celine Get Jar34q5l Dupes For Bag Look UMSVzp
= One & Two; var value: Both = { foo: 1, bar: trueMini Femme Bowling Guess Noir 963536 Sac Isla j3RSA54cLq };

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
typeThe Luxury Ultimate To Guide Less Celine Get Jar34q5l Dupes For Bag Look UMSVzp One = { prop: number };
type TwoDe En Métal RabanneArzadesign La Robe Paco uF15Jc3TlK = { prop: boolean };

type Both = One & Two;

// $ExpectError
varPinko À Femme Orange 100Polyuréthanne Ou4161 Sac Main zqpUSMV 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 → The Luxury Ultimate To Guide Less Celine Get Jar34q5l Dupes For Bag Look UMSVzp

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