flow
flow copied to clipboard
Union subset still not working when it's a subset of a sentinel type
/* @flow */
type Subset = 'a' | 'b';
type A = {
sentinel: 'a';
};
type B = {
sentinel: 'b';
};
type C = {
sentinel: 'c';
contents: string;
};
type Fullset = A | B | C;
function foo(bar: Subset): Fullset {
return ({
sentinel: bar
});
}
This fails to typecheck, but it should. Here's the error which shouldn't exist:
test.js:21
21: return ({
^ object literal. This type is incompatible with
20: function foo(bar: Subset): Fullset {
^^^^^^^ union: A | B | C
I believe it's the same issue as https://github.com/facebook/flow/issues/1014. Of course, it would be nice to hear from the developers on whether they plan to fix this in a foreseeable future...
@zgao This is working as intended. Unfortunately without an assurance that sentinel is read-only, we cannot conclude that { sentinel: 'a' | 'b' } is the same as { sentinel: 'a' } | { sentinel: 'b' }.
function gen(sentinel: 'a' | 'b') {
const obj = { sentinel };
sub(obj, () => {
obj.sentinel = 'b';
});
}
gen('a');
function sub(obj: { sentinel: 'a' } | { sentinel: 'b' }, callback: () => void) {
if (obj.sentinel === 'a') fix(obj, callback);
}
function fix(obj: { sentinel: 'a' }, callback: () => void): {
callback();
(obj.sentinel: 'a');
}