Flow: Add Intl defintions

Created on 12 Jan 2016  路  5Comments  路  Source: facebook/flow

Should go in lib/intl.js or lib/ecma402.js

http://www.ecma-international.org/ecma-402/2.0/

It seems like TypeScript moved this out of .d.ts to make it part of the core compiler.

Library definitions

Most helpful comment

I've been using the following locally. It's not complete, but it's a start.

declare var Intl: {
  Collator: Class<Collator>,
  DateTimeFormat: Class<DateTimeFormat>,
  NumberFormat: Class<NumberFormat>
}

declare class Collator {
  constructor (
    locales?: string | string[],
    options?: CollatorOptions
  ): Collator;

  static (
    locales?: string | string[],
    options?: CollatorOptions
  ): Collator;

  compare (a: string, b: string): number;
}

type CollatorOptions = {
  localeMatcher?: 'lookup' | 'best fit',
  usage?: 'sort' | 'search',
  sensitivity?: 'base' | 'accent' | 'case' | 'variant',
  ignorePunctuation?: boolean,
  numeric?: boolean,
  caseFirst?: 'upper' | 'lower' | 'false'
}

declare class DateTimeFormat {
  constructor (
    locales?: string | string[],
    options?: DateTimeFormatOptions
  ): DateTimeFormat;

  static (
    locales?: string | string[],
    options?: DateTimeFormatOptions
  ): DateTimeFormat;

  format (a: Date | number): string;
}

type DateTimeFormatOptions = {
  localeMatcher?: 'lookup' | 'best fit',
  timeZone?: string,
  hour12?: boolean,
  formatMatcher?: 'basic' | 'best fit',
  weekday?: 'narrow' | 'short' | 'long',
  era?: 'narrow' | 'short' | 'long',
  year?: 'numeric' | '2-digit',
  month?: 'numeric' | '2-digit' | 'narrow' | 'short' | 'long',
  day?: 'numeric' | '2-digit',
  hour?: 'numeric' | '2-digit',
  minute?: 'numeric' | '2-digit',
  second?: 'numeric' | '2-digit',
  timeZoneName?: 'short' | 'long'
}

declare class NumberFormat {
  constructor (
    locales?: string | string[],
    options?: NumberFormatOptions
  ): NumberFormat;

  static (
    locales?: string | string[],
    options?: NumberFormatOptions
  ): NumberFormat;

  format (a: number): string;
}

type NumberFormatOptions = {
  localeMatcher?: 'lookup' | 'best fit',
  style?: 'decimal' | 'currency' | 'percent',
  currency?: string,
  currencyDisplay?: 'symbol' | 'code' | 'name',
  useGrouping?: boolean,
  minimumIntegerDigits?: number,
  minimumFractionDigits?: number,
  maximumFractionDigits?: number,
  minimumSignificantDigits?: number,
  maximumSignificantDigits?: number
}

All 5 comments

Any workaround for this in the meantime?

I've been using the following locally. It's not complete, but it's a start.

declare var Intl: {
  Collator: Class<Collator>,
  DateTimeFormat: Class<DateTimeFormat>,
  NumberFormat: Class<NumberFormat>
}

declare class Collator {
  constructor (
    locales?: string | string[],
    options?: CollatorOptions
  ): Collator;

  static (
    locales?: string | string[],
    options?: CollatorOptions
  ): Collator;

  compare (a: string, b: string): number;
}

type CollatorOptions = {
  localeMatcher?: 'lookup' | 'best fit',
  usage?: 'sort' | 'search',
  sensitivity?: 'base' | 'accent' | 'case' | 'variant',
  ignorePunctuation?: boolean,
  numeric?: boolean,
  caseFirst?: 'upper' | 'lower' | 'false'
}

declare class DateTimeFormat {
  constructor (
    locales?: string | string[],
    options?: DateTimeFormatOptions
  ): DateTimeFormat;

  static (
    locales?: string | string[],
    options?: DateTimeFormatOptions
  ): DateTimeFormat;

  format (a: Date | number): string;
}

type DateTimeFormatOptions = {
  localeMatcher?: 'lookup' | 'best fit',
  timeZone?: string,
  hour12?: boolean,
  formatMatcher?: 'basic' | 'best fit',
  weekday?: 'narrow' | 'short' | 'long',
  era?: 'narrow' | 'short' | 'long',
  year?: 'numeric' | '2-digit',
  month?: 'numeric' | '2-digit' | 'narrow' | 'short' | 'long',
  day?: 'numeric' | '2-digit',
  hour?: 'numeric' | '2-digit',
  minute?: 'numeric' | '2-digit',
  second?: 'numeric' | '2-digit',
  timeZoneName?: 'short' | 'long'
}

declare class NumberFormat {
  constructor (
    locales?: string | string[],
    options?: NumberFormatOptions
  ): NumberFormat;

  static (
    locales?: string | string[],
    options?: NumberFormatOptions
  ): NumberFormat;

  format (a: number): string;
}

type NumberFormatOptions = {
  localeMatcher?: 'lookup' | 'best fit',
  style?: 'decimal' | 'currency' | 'percent',
  currency?: string,
  currencyDisplay?: 'symbol' | 'code' | 'name',
  useGrouping?: boolean,
  minimumIntegerDigits?: number,
  minimumFractionDigits?: number,
  maximumFractionDigits?: number,
  minimumSignificantDigits?: number,
  maximumSignificantDigits?: number
}

Any plans to make this part of the Flow built-in library definitions?

@thetalecrafter according to specs the first arg of DateTimeFormat.prototype.format can be nullable https://www.ecma-international.org/ecma-402/2.0/#sec-datetime-format-functions

@mike1808 yeah you're right. I hadn't covered that case.

Was this page helpful?
0 / 5 - 0 ratings