Skip to content

Intl.DisplayNames options object type needs to be split #48218

Closed
@tchetwin

Description

@tchetwin

Bug Report

🔎 Search Terms

DisplayNames, Intl, locale

🕗 Version & Regression Information

The behaviour was altered in TypeScript 4.6.2, but it was differently incorrect prior to that:

⏯ Playground Link

Playground link with relevant code

💻 Code

const displayNameOptions: Partial<Intl.DisplayNamesOptions> = {
    type: "language",
    // @ts-expect-error - this should *not* be valid. The Intl API seems to cope with excess properties
    locale: "en-GB",
};

const i = new Intl.DisplayNames(["tr"], displayNameOptions);

const resolvedOptions = i.resolvedOptions();
console.log(resolvedOptions); // prints an object containing `locale`, `style`, `type`, `fallback`, `languageDisplay`

// This should be valid
resolvedOptions.languageDisplay;

// @ts-expect-error - this should not be valid
const localMatcher = resolvedOptions.localeMatcher;
console.log(localMatcher); // prints `undefined`

🙁 Actual behavior

Type errors in the above code for valid patterns, no type errors for invalid patterns.

🙂 Expected behavior

Only valid type errors, accurate representation of the API

Notes

The DisplayNamesOptions interface:

interface DisplayNamesOptions {
locale: UnicodeBCP47LocaleIdentifier;
localeMatcher: RelativeTimeFormatLocaleMatcher;
style: RelativeTimeFormatStyle;
type: "language" | "region" | "script" | "currency";
fallback: "code" | "none";
}

...is currently shared by Intl.DisplayNames() constructor, and Intl.DisplayNames.prototype.resolvedOptions(). While the underlying objects share several properties, they are not a perfect match as the types suggest.

Per MDN:

ECMA-402 links:

Shoutout to @mkubilayk who spotted this during our TypeScript update!

Suggested Fix

These types are not compatible, suggesting they should be split into something like (bikeshed) DisplayNamesOptions and DisplayNamesResolvedOptions.

Probably a Good First Issue™

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

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions