facebook / flow

Adds static typing to JavaScript to improve developer productivity and code quality.
https://flow.org/
MIT License
22.07k stars 1.86k forks source link

Intersection type of union type and literal string is not type-checking #5115

Open ghost opened 7 years ago

ghost commented 7 years ago

Hi, I don't understand why this is not type-checking. Tried in Stackoverflow without any luck. Would appreciate any help with this:

/* @flow */

type Tenums = 'a' | 'b'
type TmoreEnums = Tenums | 'c'

type Ta = Tenums & 'a'

const constA: Ta = 'a'

const fn: (TmoreEnums) => void = (v) => {
  console.log(v)
}

fn(constA)

Try

I would consider that adding an intersection type like Tenums & 'a' would be the same of having one variable of type 'a'. Probably this is expected but could anyone explain the reasoning behind this?

Replacing TmoreEnums by 'a' in the function definition would make it pass. And not calling the function would make it pass too.

Thanks a lot for this great tool.

quisido commented 7 years ago

Here's a bit more to-the-point of an example.

('a' & 'a') is incompatible with ('a' | 'b').

I'm not if it's bug or expected behavior, but it's confirmed a true statement.