Skip to content

Various Intl-related constructors and methods reject Intl.Locale objects in locales parameter #52946

@lionel-rowe

Description

@lionel-rowe

lib Update Request

Various Intl-related constructors and methods reject Intl.Locale objects in their locales parameter.

Related to #47802 (Signature for toLocale[X]String is missing Intl.Locale), but the updated signature is still missing for various other Intl-related constructors and methods.

Draft fix here:
main...lionel-rowe:TypeScript:fix/locales-argument

I can submit as a PR once this issue is accepted, or happy for someone else to pick it up otherwise.

Configuration Check

My compilation target is ES2022 and my lib is the default.

Missing/Incorrect Definition

Various, including but not limited to:

  • String#toLocaleUpperCase
  • Intl.Collator constructor
  • Intl.DateTimeFormat constructor
  • Intl.ListFormat constructor
  • Intl.Segmenter constructor

Sample Code

const locale = new Intl.Locale('es')

// these now work as expected, thanks to https://github.com/microsoft/TypeScript/pull/47811
0 .toLocaleString(locale)
new Date(0).toLocaleDateString(locale)

// ...but these all give `Argument of type 'Locale' is not assignable to parameter of type 'string | string[] | undefined'.`
// All function as expected without throwing in JS (browser/node/deno)
'x'.toLocaleUpperCase(locale)
new Intl.Collator(locale)
new Intl.DateTimeFormat(locale)
new Intl.ListFormat(locale)
new Intl.Segmenter(locale, { granularity: 'word' })

Documentation Link

https://tc39.es/ecma402/#sec-canonicalizelocalelist

CanonicalizeLocaleList is called on all such locales arguments, iterating through them as an array, checking for the [[InitializedLocale]] internal slot (used to identify Intl.Locale objects), and reading the [[Locale]] internal slot if it exists (for Intl.Locale objects, this is the string representation of the locale). Thus 'x'.toLocaleUpperCase('es') behaves identically to 'x'.toLocaleUpperCase(new Locale('es'))

🔎 Search Terms

  • locales
  • Intl.Locale
  • LocalesArgument
  • CanonicalizeLocaleList

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do this

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions