Search code examples
flowtype

Can't extends imported class


class A {}
class B extends A {}

the code above work fine but when I try to do the same with an imported class, I't does not work.

declare module 'a' {
  declare export class A {}
}

import typeof { A } from 'a';

class B extends A {}

Cannot reference type A [1] from a value position

can anyone solve this ?

https://flow.org/try/#0CYUwxgNghgTiAEBbA9sArhBByKX4G8AoeeUSWBEADwAdkYAXecgZxfgEECBfQ3wgJaI6jeAwCeNEMgBmBTvG4ACmTGSJ4OLAG5ChVuwBC8agxAA7YOy75uQA


Solution

  • When you define a class you actually define both a type and a Javascript class, and both have the same name. Types only exist at compile time - they have no runtime representation. In your example you imported the type, but not the runtime value.

     // imports the type only
    import typeof { A } from 'a'
    
     // imports both the type and the runtime value
    import { A } from 'a'
    

    The type informs Flow what the class does for purposes of type checking. But the runtime value is what defines the implementation to be executed. The compiled program must have a reference to the runtime value to instantiate or to extend a class. In other words, remove the typeof keyword and it should work.