Flow: Union types are not applied on properties

Created on 3 Apr 2018  路  5Comments  路  Source: facebook/flow

I expect properties to inherit union types

type Student = {
    type: 'student'
}

type Teacher = {
    type: 'teacher'
}

const getType = (someone : Student | Teacher) =>  someone.type;

const type = getType({ type: 'student' });

('student' : typeof type);
// ^ string literal `teacher` [1] is incompatible with string literal `student` [2].
unionintersections bug regression

Most helpful comment

Seems as though whilst flow says it's inferring the type of type here as 'student' | 'teacher' it's not checking with it correctly (or something along those lines).

Set up 3 different scenarios in a flowtry.
As displayed there it seems partially connected to the way flow resolves the return type. And even though it does say it has the correct type there, it's checking on it wrongly.

Just leaving this here as my findings on the subject whilst I was messing about with it. Seems like a bug worth fixing.

All 5 comments

Not a bug.

Type of type is "teacher" | "student" as expected.

Yes, but why doesn't "student" conform to it?

Well, looks like a problem indeed. You can remove the error if you type the function return type explicitly though.

Seems as though whilst flow says it's inferring the type of type here as 'student' | 'teacher' it's not checking with it correctly (or something along those lines).

Set up 3 different scenarios in a flowtry.
As displayed there it seems partially connected to the way flow resolves the return type. And even though it does say it has the correct type there, it's checking on it wrongly.

Just leaving this here as my findings on the subject whilst I was messing about with it. Seems like a bug worth fixing.

Regression from Flow 0.55.0

Was this page helpful?
0 / 5 - 0 ratings