flow icon indicating copy to clipboard operation
flow copied to clipboard

Special type operator allowing for "instance of"

Open zgao opened this issue 10 years ago • 9 comments

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)

zgao avatar Feb 10 '16 16:02 zgao

I just hit the same issue, and ended up using this workaround:

declare var thing: Thing;
type ThingType = typeof thing;

dchambers avatar Sep 20 '16 07:09 dchambers

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.

A demonstration:

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();

STRML avatar Sep 21 '16 18:09 STRML

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;
}

STRML avatar Feb 06 '18 19:02 STRML

+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]

aksuska avatar Mar 10 '18 16:03 aksuska

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 .

STRML avatar Mar 10 '18 17:03 STRML

Yes, for a variety of reasons:

  1. You copied/pasted the extraction code incorrectly and omitted several type arguments
  2. You provided no type argument what for what you thought classRef was an instance of
  3. You did not actually instantiate MyObject

Try this running example.

See also this example which addresses the question in OP.

STRML avatar Mar 10 '18 19:03 STRML

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>>

aksuska avatar Mar 10 '18 20:03 aksuska

@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?

aksuska avatar Mar 12 '18 14:03 aksuska

It can now be achieved with conditional type

SamChou19815 avatar Aug 06 '24 15:08 SamChou19815