Flow: Create a flow type by extending another type

What you’re looking for is the intersection type. According to the documentation:

An intersection type requires a value to be all of the input types.

Syntax: Intersection: < type 1 > & < type 2 > … & < type n >

The intersection type is intended to extend an existing type and add additional type requirements to it.

type someType = {
  keyOne: string,
  keyTwo: string
}

type someOtherType = someType & {
  keyThree: string
}

const shouldBeOk: someOtherType = {
  keyOne: 'biz',
  keyTwo: 'buzz',
  keyThree: 'baz',
}

const shouldError: someOtherType = {
  keyOne: 123,
  keyTwo: 'hello',
  keyThree: 'world',
}

// flow error:
16: const shouldError: someOtherType = {
                               ^ object literal. This type is incompatible with
8: type someOtherType = someType & {
                        ^ object type

The logical opposite of the intersection type is the union type. According to the documentation:

A union type requires for a value to be one of the input types.

Syntax: Union: < type 1 > | < type 2 > … | < type n >

As an example. you can use the union type to create an enumerable.

type fooBarBazType="foo" | 'bar' | 'baz';
const shouldBeOk: fooBarBazType="bar";

const shouldError: fooBarBazType="buzz";

4: const shouldError: fooBarBazType="buzz";
                                      ^ string. This type is incompatible with
4: const shouldError: fooBarBazType="buzz";
                      ^ string enum

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)