/* @flow */
import type Events from 'events';
declare class Foo extends Events.EventEmitter {
bar(): number;
}
const f = new Foo();
f.on('bar', ()=>{});
In Flow 0.20, this gives an error:
test.js:5
5: declare class Foo extends Events.EventEmitter {
^^^^^^ Events. type referenced from value position
3: import type Events from 'events';
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type Events
Found 1 error
Works fine with Flow 0.19.1.
Please try:
/* @flow */
import { EventEmitter } from 'events';
declare class Foo extends EventEmitter {
bar(): number;
}
const f = new Foo();
f.on('bar', ()=>{});
As far as I can tell, the fact that it was working before was a bug.
I specifically wanted import type. It's been very useful to me when I don't want to import a module (so I can avoid its execution and side effects being taken immediately, or so I can avoid increasing the size of a javascript bundle) but the module is still involved in the type signatures. This worked correctly before. Both import type and declare class are purely for Flow and both are removed during compilation and seem like a natural fit.
This bug-slash-feature seems to extend _(no pun intended)_ to definition file.
As in, in definition file, I cannot declare one type extending another type, because "type referenced in value position". Even when that makes zero sense in definition file.
See the immutable-js discussion that linked here
As far as I can tell, you cannot now declare any kind of type hierarchy in the .js definition file, since it is always "type referenced in value position". It seems like a bug to me.
As @avikchaudhuri mentioned, it's an error to use a type as the argument to extends. This is part of JavaScript class syntax and isn't stripped away by Babel, so using a type there would cause a runtime error.
Let me know if there's something I missed here. Thanks!
I'm using declare class. The whole class declaration is stripped away by Babel, so there wouldn't be a runtime error.
Oh, I see. I've wanted this in the past, too. Thanks for clarifying!
Any news on this? Fairly significant bug
It looks like this works now. Let me know if you still have issues 馃憤
Isn't this still an issue when the types exist in different modules? Note, I've tried both import type and import typeof. My use case is extending Component from react in a definition file.
declare module 'foo' {
declare class Foo {}
}
declare module 'bar' {
import typeof { Foo } from 'foo';
declare class Bar extends Foo {}
}
This is still a bug on v0.132
Most helpful comment
Isn't this still an issue when the types exist in different modules? Note, I've tried both
import typeandimport typeof. My use case is extendingComponentfromreactin a definition file.