Typescript: [i18n] Intl getCanonicalLocales / RelativeTimeFormat / ListFormat > 2339 error

Created on 22 Dec 2018  ·  10Comments  ·  Source: microsoft/TypeScript

TypeScript Version: 3.2.2

Search Terms:
Intl.getCanonicalLocales TS TypeScript Error 2339 undefined i18n

Code

Intl.getCanonicalLocales('EN-US');

Expected behavior:
Code works, and returns ["en-US"]
https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Intl/getCanonicalLocales

Actual behavior:
TS Error 2339, the property getCanonicalLocales does not exists on Intl

lib.d.ts In Discussion Suggestion

Most helpful comment

It is now stage 4.

All 10 comments

Same thing with

Code

new Intl.RelativeTimeFormat('en');

Expected behavior:
Code works
https://developers.google.com/web/updates/2018/10/intl-relativetimeformat

Actual behavior:
TS Error 2339, the property RelativeTimeFormat does not exists on Intl

Same thing with

Code

new Intl.ListFormat('en');

Expected behavior:
Code works
https://developers.google.com/web/updates/2018/12/intl-listformat

Actual behavior:
TS Error 2339, the property ListFormat does not exists on Intl

How can I extend the Intl object in a types.d.ts file ?

Drafts are not included into libs. You can declare a d.ts file with the definitions:

declare namespace Intl {
    function getCanonicalLocales(locales: string | string[]): string[];

    class RelativeTimeFormat {
        constructor(locale: string);

        // TODO Add other properties/methods
    }
    // const RelativeTimeFormat: any; // Use this instead of the class if you don't want to declare all properties/methods

    class ListFormat {
        constructor(locale: string);

        // TODO Add other properties/methods
    }
    // const ListFormat: any; // Use this instead of the class if you don't want to declare all properties/methods
}

Thank you !

Administrators will close this issue when the features are definitively implemented.
For now, the workaround works like a charm.

Is there any update on this? RelativeDateTime should is now more than a draft: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RelativeTimeFormat

Has Intl.ListFormat been added for support?

It is now stage 4.

One of the polyfills for Intl.ListFormat has its own declaration file that you can use in the meantime.

RelativeTimeFormat has already been added.

39662 is pending for getCanonicalLocales

Was this page helpful?
0 / 5 - 0 ratings