Skip to content

Commit

Permalink
react-intl libdef: Add typeof to Intl.PluralRules in some places
Browse files Browse the repository at this point in the history
This fixes one of the things Flow glosses over and would catch when
we migrate this to a .js.flow file, with an error like this:

  Error -------------------- flow-typed/react-intl_vx.x.x.js:353:36

  Cannot use `PluralRules` as a type. A name can be used as a type
  only if it refers to a type, interface, class, or enum definition.
  To get the type of a non-class value, use `typeof`.
  [value-as-type]

     353|     pluralRules: {| [key: string]: Intl.PluralRules |};
                                             ^^^^^^^^^^^^^^^^

I think this is what's going on:

Flow treats Intl.Foo as a value, because Intl is a `declare var`,
not a `declare type` [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[],
    ...
  }

I think that means that if we want to use the declared type of an
Intl.Foo, we'd normally want to use `typeof Intl.Foo`.

But most of the actual Intl members with `Class<…>` are special,
because a class value can be used as a type without `typeof`.

I think Intl.PluralRules doesn't get that special treatment, because
it's got `?Class<…>` -- which expands to the union type
`Class<…> | null | void`. Union types aren't class definitions, so
the special treatment doesn't apply and we should use `typeof`.

[1] https://github.com/facebook/flow/blob/v0.141.0/lib/intl.js#L8-L15
  • Loading branch information
chrisbobbe committed Mar 25, 2022
1 parent 7a27cb7 commit 2bb39a3
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions flow-typed/react-intl_vx.x.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ declare module 'react-intl' {
number: {| [key: string]: Intl.NumberFormat |};
message: {| [key: string]: IntlMessageFormat |};
relativeTime: {| [key: string]: RelativeTimeFormat |};
pluralRules: {| [key: string]: Intl.PluralRules |};
pluralRules: {| [key: string]: typeof Intl.PluralRules |};
list: {| [key: string]: ListFormat |};
displayNames: {| [key: string]: DisplayNames |};
}
Expand Down Expand Up @@ -412,7 +412,7 @@ declare module 'react-intl' {
formatPlural(
value: number, // Param of Intl.PluralRules.prototype.select()
opts?: FormatPluralOptions,
): $Call<<R>((...args: any[]) => R) => R, $PropertyType<Intl.PluralRules, 'select'>>;
): $Call<<R>((...args: any[]) => R) => R, $PropertyType<typeof Intl.PluralRules, 'select'>>;
formatMessage(
descriptor: MessageDescriptor,
// The `+` was added to make the properties covariant rather
Expand Down

0 comments on commit 2bb39a3

Please sign in to comment.