-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Object intersections: broken or underdocumented? #1366
Comments
See #1331 -- there are many outstanding issues with intersections right now, but hopefully they'll be addressed soon. It seems to me that in your case the root of the issue is that Flow believes (incorrectly) that an intersected type may be in only one of the subtypes at a time, which is the first bug on that list. |
I saw that meta-issue, but didn't quite understand the title of #1327 to be describing my issue. Thanks! |
I'm having an issue which I believe to be similar, or pretty much the same. I'm seeing the following error:
For this code: type EntryCollectionResponse = {
total: number,
skip: number,
limit: number,
items: Array<Entry>,
includes?: IncludesCollection
}
/**
* EntryCollection type
*/
export type EntryCollection = EntryCollectionResponse & {
toPlainObject: () => Object
}
export function wrapEntryCollection (data: EntryCollectionResponse): EntryCollection {
const wrappedData: EntryCollection = mixinToPlainObject(cloneDeep(data))
if (wrappedData.includes) {
mixinLinkGetters(wrappedData)
}
return wrappedData
} However, if I just duplicate all the properties in EntryCollectionResponse inside of EntryCollection, it works fine. |
@trodrigues Seems like |
Ah, sorry, those are just other modules that get imported. cloneDeep is from lodash (so, not typed, and I don't have any typing definitions added for it), and the mixin is fairly straightforward: export default function mixinToPlainObject (data) {
return Object.defineProperty(data, 'toPlainObject', {
enumerable: false,
configurable: false,
writable: false,
value: function () {
return cloneDeep(this)
}
})
} The wrapEntryCollection method essentially just takes a server response and then adds some methods to it like So essentially, my plan was to have an EntryCollectionResponse which represents the response received from the server, and then EntryCollection just extends that with the additional stuff that gets added by the wrapper method. |
Are there still issues here? |
As of 0.22.1, the node streams example works, so that's nice! I'll check with our internal codebase later, but closing this in the meantime. |
Confirmed: 0.22.1 is happy about the object intersections in our internal codebase as well. Thank you very much for the fixes! |
Since upgrade from 0.20.1 to 0.21.0, Flow has been complaining about our use of object intersections (though I'm not sure Flow 0.21.0 actually introduced a behaviour change -- it might just be surfacing a previously swallowed error now).
Indeed I'm not sure that we're using intersections correctly. The docs are a bit sparse, so I took a look at how Flow uses them in its library definitions. A good example seem to be the argument definitions for Node's
stream
classes. Here they are, formatted slightly differently for readability:It is my understanding that an object of type
DuplexStreamOptions
will also conform toReadableStreamOptions
andWritableStreamOptions
. In fact, that's how thestreams.Duplex
constructor in Node treats it. Quoting the node docs on this:So if I have a function that consumes a
DuplexStreamOptions
, shouldn't it be allowed to access properties from all three sub-types? E.g.:However, Flow isn't happy with that:
and I'm not sure how to make Flow happy. Neither this:
nor this:
seems to work.
But it gets weirder. If I simulate the way node would use these types, it all seems to work just fine:
I can even make
Duplex
use properties from all three sub-types without problems:BUT: I can't use properties from different subtypes in the same expression:
fails in the same way as the
hasObjectMode
example above.So, are object intersections slightly broken, or am I doing something wrong?
The text was updated successfully, but these errors were encountered: