flow icon indicating copy to clipboard operation
flow copied to clipboard

Not recognizing Intl as a built-in 0bject object

Open StokeMasterJack opened this issue 10 years ago • 7 comments

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

StokeMasterJack avatar Nov 13 '16 17:11 StokeMasterJack

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;

kumar303 avatar May 30 '17 20:05 kumar303

Has someone from Flow team looked at this issue?

Fernando-Abreu avatar Jun 05 '17 17:06 Fernando-Abreu

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;

fitzk avatar Jun 07 '17 18:06 fitzk

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.

maxsiml avatar Jul 23 '17 20:07 maxsiml

Is there a central repo for flowtypes of built-in browser objects?

dashed avatar Dec 10 '17 06:12 dashed

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.

dawsbot avatar Feb 09 '18 16:02 dawsbot

Same issue just now with Intl.RelativeTimeFormat. @dawsbot window.Intl... fixed the flow error though.

campbellgoe avatar Dec 21 '19 21:12 campbellgoe