typescript-vs-flow icon indicating copy to clipboard operation
typescript-vs-flow copied to clipboard

Intersection of unions

Open agentcooper opened this issue 7 years ago • 3 comments

type Base = { name: string }

type Alternative =
    { type: 'A', value: string } |
    { type: 'B', value: number };

type Final = Alternative & Base;

const arr: Final[] = [
  { type: 'A', value: '1', name: 'c' }
];

const first: Final = arr[0]

Typescript 2.3 correctly shows no errors.

Flow 0.47.0 shows an error.

agentcooper avatar May 26 '17 09:05 agentcooper

Changing Final to use a type spread seems to work:

type Final = { ...Alternative, ...Base }

@vkurchatkin Any idea why Flow rejects the example from @agentcooper but not the type spread?

dsilvasc avatar Jan 15 '18 00:01 dsilvasc

@dsilvasc intersection operator creates intersection types for properties (type: 'A' & 'B'), that's a totally different thing than spread operator, which just overrides from left to right (type: 'B').

Chudesnov avatar Jan 15 '18 10:01 Chudesnov

@Chudesnov but with the spread definition of Final, flow accepted type: 'A' in arr and in first.

dsilvasc avatar Jan 15 '18 10:01 dsilvasc