-
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
Type for constructor function #1409
Comments
Unfortunately this isn't actually intended to work -- but we're giving a poor error message for it :( Because classes are typed nominally in Flow, this error message should really say that it is invalid to use Sorry for the confusion here -- I don't think we actually have a way to generically specify structural class types. Maybe you can go into your use case a bit so we can understand it better? |
@jeffmo I see, I probably misunderstood what
It should probably say "instance of class T" Although it's weird, cause if I add
It's simple: I want to have a map of constructors that produce objects of type To be precise, I don't need class type, I need a constructor type, i.e. a function that can be called with I started with the following: const map: { [key: string]: Class<MyType> } = { foo: MyConstructor } Since it didn't work, I ended up wrapping constructors with functions: const map: { [key: string]: (arg: any) => MyType } = { foo: arg => new MyConstructor(arg) } It kind of makes sense, since |
Good point, I'll fix this wording. Thanks
Yes, this is actually a bug in Flow. We've known about it for a bit, but it's been so obscure that we haven't gotten to it yet.
This makes a lot of sense, thanks. I'll log this as a feature request |
…eturn Here's Flow's built-in definition for the `Intl` object [1]: declare var Intl: { Collator: Class<Intl$Collator>, DateTimeFormat: Class<Intl$DateTimeFormat>, NumberFormat: Class<Intl$NumberFormat>, PluralRules: ?Class<Intl$PluralRules>, getCanonicalLocales?: (locales?: Intl$Locales) => Intl$Locale[], ... } (It uses Flow's built-in `Class` type: https://flow.org/en/docs/types/utilities/#toc-class ) So, for Intl, the names with $ stand for instance types, and the names with . stand for class types. It looks like the touched lines of code really want to say something like getDateTimeFormat: ConstructorOf<Intl.DateTimeFormat> , where `ConstructorOf` is a name I just made up, and Flow doesn't yet have a tool that would fit that name; see facebook/flow#1409 (comment) Without that, it's at least easy to preserve the return value of each of these functions. As with any constructor function, the return value is an instance object, not the class object, which is why we make this change. [1] https://github.com/facebook/flow/blob/v0.141.0/lib/intl.js#L8-L15
…eturn Here's Flow's built-in definition for the `Intl` object [1]: declare var Intl: { Collator: Class<Intl$Collator>, DateTimeFormat: Class<Intl$DateTimeFormat>, NumberFormat: Class<Intl$NumberFormat>, PluralRules: ?Class<Intl$PluralRules>, getCanonicalLocales?: (locales?: Intl$Locales) => Intl$Locale[], ... } (It uses Flow's built-in `Class` type: https://flow.org/en/docs/types/utilities/#toc-class ) So, for Intl, the names with $ stand for instance types, and the names with . stand for class types. It looks like the touched lines of code really want to say something like getDateTimeFormat: ConstructorOf<Intl.DateTimeFormat> , where `ConstructorOf` is a name I just made up, and Flow doesn't yet have a tool that would fit that name; see facebook/flow#1409 (comment) Without that, it's at least easy to preserve the return value of each of these functions. As with any constructor function, the return value is an instance object, not the class object, which is why we make this change. [1] https://github.com/facebook/flow/blob/v0.141.0/lib/intl.js#L8-L15
It seems that
Class
type is not covariant:Given
This fails:
const Clazz: Class<AType> = A;
The text was updated successfully, but these errors were encountered: