Skip to content

Instances of Intl.NumberFormat and Intl.DateTimeFormat does not expose the format() function #1911

@kivle

Description

@kivle

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[];
    }

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptFixedA PR has been merged for this issueHelp WantedYou can do thisRevisitAn issue worth coming back to

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions