typescript-vs-flow
typescript-vs-flow copied to clipboard
Intersection of unions
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]
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 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 but with the spread definition of Final
, flow accepted type: 'A'
in arr
and in first
.