Flow: Not recognizing Intl as a built-in 0bject object

Created on 13 Nov 2016  路  7Comments  路  Source: facebook/flow

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
Library definitions

Most helpful comment

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.

All 7 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jamesisaac picture jamesisaac  路  44Comments

NgoKnows picture NgoKnows  路  40Comments

blax picture blax  路  83Comments

STRML picture STRML  路  48Comments

jlongster picture jlongster  路  55Comments