Narley Brittes
Narley Brittes

Reputation: 151

Why flowjs doesn't throw error on empty array when array must be of a type?

Can't understand why the following code pass flow check:

type Foo = "A" | "B" | "C"

const myFoo: Array<Foo> = []

Isn't Array<Foo> enforcing the array to have some Foo type?

I'm a bit confused.

Flow repl: https://flow.org/try/#0C4TwDgpgBAYg9nKBeKAiAgqqAfNAhLXVAYVQCgyBjOAOwGdgoBbEeOALinQCduBDEAB42APmRQA2gF0yQA

Upvotes: 1

Views: 232

Answers (1)

user11307804
user11307804

Reputation: 828

The type Array<Foo> means that all elements in the array must have type Foo. This is true for the empty array []: all elements in this array have type Foo. It just so happens that there are no elements in the array. Flow will enforce that all elements added to the array will be of type Foo.

Upvotes: 2

Related Questions