Skip to content

Make Intl locales arrays readonly #56513

Closed
@Shraymonks

Description

@Shraymonks

⚙ Compilation target

Any

⚙ Library

intl.d.ts

Missing / Incorrect Definition

The locales array should be typed as a readonly array in Intl constructors and methods.

Currently they typed as normal arrays which causes a type error when passing a readonly array such as navigator.languages.

Resulting in the following:
Argument of type 'readonly string[]' is not assignable to parameter of type 'string | string[] | undefined'. The type 'readonly string[]' is 'readonly' and cannot be assigned to the mutable type 'string[]'.(2345)

There was a previous PR #50135 that fixed it for one particular call site. However there are still several instances that also have this issue:

function getCanonicalLocales(locale?: string | string[]): string[];

new (locales?: string | string[], options?: PluralRulesOptions): PluralRules;
(locales?: string | string[], options?: PluralRulesOptions): PluralRules;
supportedLocalesOf(locales: string | string[], options?: { localeMatcher?: "lookup" | "best fit"; }): string[];

locales?: UnicodeBCP47LocaleIdentifier | UnicodeBCP47LocaleIdentifier[],

locales?: UnicodeBCP47LocaleIdentifier | UnicodeBCP47LocaleIdentifier[],

new (locales?: BCP47LanguageTag | BCP47LanguageTag[], options?: ListFormatOptions): ListFormat;

supportedLocalesOf(locales: BCP47LanguageTag | BCP47LanguageTag[], options?: Pick<ListFormatOptions, "localeMatcher">): BCP47LanguageTag[];

new (locales?: BCP47LanguageTag | BCP47LanguageTag[], options?: SegmenterOptions): Segmenter;

supportedLocalesOf(locales: BCP47LanguageTag | BCP47LanguageTag[], options?: Pick<SegmenterOptions, "localeMatcher">): BCP47LanguageTag[];

Sample Code

new Intl.DateTimeFormat(navigator.languages)

Documentation Link

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#using_the_browsers_preferred_language

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptGood First IssueWell scoped, documented and has the green lightHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions