-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Description
I'm currently working on converting our product at work to use Typescript 1.4. One Javascript feature we use quite extensively is the possibility to make instances of Intl.NumberFormat and Intl.DateTimeFormat objects in the Intl namespace based on a specified culture. Example:
var formatter = new Intl.NumberFormat("zh-Hans-CN-u-nu-hanidec",
{ useGrouping: true, maxDecimals: 3});
var numberAsString = formatter.format(123321.456); // Property 'format' does not exist on type 'Collator'According to MDN this is perfectly valid in modern browsers (except for Safari), and it's working well for us.
However, the format() function is currently not exposed from the returned object with the Typescript mappings in Intl.d.ts. When you instantiate an Intl.NumberFormat object, the returned object is of type Collator instead of NumberFormat. Same problem for the Intl.DateTimeFormat object.
My suggestion is that the NumberFormat and DateTimeFormat interfaces should extend Collator, and the constructor function should return the relevant object interface that extends Collator. For example:
interface NumberFormat extends Collator {
format(value: number): string;
resolvedOptions(): ResolvedNumberFormatOptions;
}
var NumberFormat: {
// The following four functions return NumberFormat objects
new (locales?: string[], options?: NumberFormatOptions): NumberFormat;
new (locale?: string, options?: NumberFormatOptions): NumberFormat;
(locales?: string[], options?: NumberFormatOptions): NumberFormat;
(locale?: string, options?: NumberFormatOptions): NumberFormat;
supportedLocalesOf(locales: string[], options?: NumberFormatOptions): string[];
supportedLocalesOf(locale: string, options?: NumberFormatOptions): string[];
}