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

Not recognizing Intl as a built-in 0bject object #2801

Closed
StokeMasterJack opened this Issue Nov 13, 2016 · 6 comments

Comments

Projects
None yet
8 participants
@StokeMasterJack

StokeMasterJack commented Nov 13, 2016

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
@kumar303

This comment has been minimized.

Show comment
Hide comment
@kumar303

kumar303 May 30, 2017

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 commented May 30, 2017

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;
@Fernando-Abreu

This comment has been minimized.

Show comment
Hide comment
@Fernando-Abreu

Fernando-Abreu Jun 5, 2017

Has someone from Flow team looked at this issue?

Fernando-Abreu commented Jun 5, 2017

Has someone from Flow team looked at this issue?

@fitzk

This comment has been minimized.

Show comment
Hide comment
@fitzk

fitzk Jun 7, 2017

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 commented Jun 7, 2017

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;
@vashchukmaksim

This comment has been minimized.

Show comment
Hide comment
@vashchukmaksim

vashchukmaksim Jul 23, 2017

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.

vashchukmaksim commented Jul 23, 2017

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.

@dashed

This comment has been minimized.

Show comment
Hide comment
@dashed

dashed Dec 10, 2017

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

dashed commented Dec 10, 2017

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

@dawsbot

This comment has been minimized.

Show comment
Hide comment
@dawsbot

dawsbot Feb 9, 2018

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 commented Feb 9, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment