user1299412
user1299412

Reputation: 409

Flow - array of subtypes

Having two objects, where B extends A, I can assign single instance of B to variable of type A. However, with arrays, that is not possible. Is there anything I can do to make this work? Thanks!

// @flow
type ObjectA = { foo: string };
type ObjectB = ObjectA & {bar: number };

let objectBArray: ObjectB[] = [{ foo: 'test', bar: 42 }];
let objectAArray: ObjectA[] = objectBArray; // Does not work!

Upvotes: 3

Views: 372

Answers (1)

user1299412
user1299412

Reputation: 409

Found the answer myself:

let objectAArray: Array<$Subtype<ObjectA>> = objectBArray;

Not descriped in the docs, but seems to be official part of the API https://flow.org/en/docs/types/utilities/#toc-subtype

Upvotes: 2

Related Questions