flow seems to recognize other built-in objects like Event. But it does not recognize Intl:
const currencyFormatter = new Intl.NumberFormat("en-US", {style: 'currency', currency: 'USD'});
^^^^ identifier `Intl`. Could not resolve name
This is a [very incomplete] definition I'm using:
declare class Intl$NumberFormat {
constructor(locales: string | Array<string>, options?: Object): void;
format(number: Number): string;
}
declare type IntlType = {
NumberFormat: Class<Intl$NumberFormat>,
}
// Mark it as possibly undefined since
// Safari and some recent versions of Firefox for Android do not implement it.
declare var Intl: typeof undefined | IntlType;
Has someone from Flow team looked at this issue?
If you just want to get rid of the error, this is what I did:
type IntlType = any;
const globalIntl: IntlType = window.Intl;
const tz = new globalIntl.DateTimeFormat().resolvedOptions().timeZone;
I got an error with @kumar303 definition that complains about "NumberFormat not found in statics..." and write another version that works for me.
type Intl$NumberFormat = {
(locales: string | Array<string>, options?: Object): Intl$NumberFormat,
format: (val: number) => string
}
type IntlType = {
NumberFormat: Intl$NumberFormat
}
declare var Intl: IntlType;
I think it can be written more accurate but it checks all types correctly.
Is there a central repo for flowtypes of built-in browser objects?
Just added "window." before "Intl" 馃帀
const currencyFormatter = new window.Intl.NumberFormat("en-US", {style: 'currency', currency: 'USD'});
This means it won't work for server-rendered apps, unless you polyfill window.
Same issue just now with Intl.RelativeTimeFormat. @dawsbot window.Intl... fixed the flow error though.
Most helpful comment
Just added "
window." before "Intl" 馃帀This means it won't work for server-rendered apps, unless you polyfill
window.