Special type operator allowing for "instance of"
Suppose I am passing into my HOC a react class and a couple functions that expect a member of that class. For example, let's say I have a CodeField with a cursorPosition.
I want Flow to be able to typecheck HOC(CodeField, (cfInstance) => cfInstance.getCursorPosition()) - meaning that I want some InstanceOf<T> operator that allows me to do:
function HOC<T: ReactClass, U>(component: T, f: (inst: InstanceOf<T>) => U)
I just hit the same issue, and ended up using this workaround:
declare var thing: Thing;
type ThingType = typeof thing;
Ran into the same issue when writing a typedef, issue is that there's an object floating around where its properties are classes, not instances of classes. I wanted to use $PropertyType to get the type of an instance but had no luck.
class Foo {}
const a: Foo = new Foo()
type FooClass = Class<Foo>;
// What should this be if we only have FooClass?
const b: $InstanceOf<FooClass> = new Foo();
I'm happy to report you can do this now with $Call, and it works great!
declare type InstanceOfClassExtractor = <C>(Class<C>) => C;
declare type InstanceOf<Cls: Class<any>> = $Call<InstanceOfClassExtractor, Cls>;
For instance, to work around import restrictions in type declarations, I sometimes use $Exports<pkgName>. So if I wanted to declare something as an instance of e.g. EventEmitter3:
class Foo {
addEmitter(emitter: InstanceOf<$Exports<'eventemitter3'>>): void;
}
+1 for having a simpler and more direct way of accomplishing this. [removed incorrect statements about @STRML solution as they don't add to this thread]
Could you elaborate on "couldn't get it to work"?
On Mar 10, 2018 10:25 AM, "aksuska" [email protected] wrote:
+1. Also the solution posted by @STRML https://github.com/strml does not address the main concern of this thread. I can't even get it to work for the use case he brings up given the code provided: i.e. variable/property as a class object, rather than an instance of a class (object of class type).
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/facebook/flow/issues/1388#issuecomment-372042451, or mute the thread https://github.com/notifications/unsubscribe-auth/ABJFPxX0zpvo6uLbJhHFCNYDPia-i0K-ks5tc_6MgaJpZM4HXcCl .
Yes, for a variety of reasons:
- You copied/pasted the extraction code incorrectly and omitted several type arguments
- You provided no type argument what for what you thought
classRefwas an instance of - You did not actually instantiate
MyObject
See also this example which addresses the question in OP.
The first example shows what I was missing. Your original solution did not show a concrete example so my tests were broken due to my incomplete understanding of Flow syntax. In particular the concrete usage of your example is: InstanceOf<Class<Class_Name>>
@STRML Did you also figure out how to declare inheritable class types, i.e. where a property or return type is a class of a class that extends the class, rather than an instance?
It can now be achieved with conditional type