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

Union types are not applied on properties #6085

Open iddan opened 6 years ago

iddan commented 6 years ago

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].
apsavin commented 6 years ago

Not a bug.

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

iddan commented 6 years ago

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

apsavin commented 6 years ago

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

M-TGH commented 6 years ago

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.

goodmind commented 5 years ago

Regression from Flow 0.55.0