From 739ab63567065fc18fb486930a6f5166386e4e94 Mon Sep 17 00:00:00 2001 From: Andrii Furmanets Date: Wed, 29 Apr 2026 21:53:28 +0300 Subject: [PATCH] Fix Intl.Collator compare declaration --- src/lib/es5.d.ts | 2 +- .../intlCollatorCompareAccessor.errors.txt | 25 +++++++++++++++++++ .../compiler/intlCollatorCompareAccessor.ts | 15 +++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/intlCollatorCompareAccessor.errors.txt create mode 100644 tests/cases/compiler/intlCollatorCompareAccessor.ts diff --git a/src/lib/es5.d.ts b/src/lib/es5.d.ts index ced21a6d72af1..eedba9e8809fa 100644 --- a/src/lib/es5.d.ts +++ b/src/lib/es5.d.ts @@ -4419,7 +4419,7 @@ declare namespace Intl { } interface Collator { - compare(x: string, y: string): number; + readonly compare: (this: void, x: string, y: string) => number; resolvedOptions(): ResolvedCollatorOptions; } diff --git a/tests/baselines/reference/intlCollatorCompareAccessor.errors.txt b/tests/baselines/reference/intlCollatorCompareAccessor.errors.txt new file mode 100644 index 0000000000000..8f48d7cea48c3 --- /dev/null +++ b/tests/baselines/reference/intlCollatorCompareAccessor.errors.txt @@ -0,0 +1,25 @@ +intlCollatorCompareAccessor.ts(7,7): error TS2322: Type '(this: Intl.Collator, x: string, y: string) => number' is not assignable to type '(this: void, x: string, y: string) => number'. + The 'this' types of each signature are incompatible. + Type 'void' is not assignable to type 'Collator'. +intlCollatorCompareAccessor.ts(11,10): error TS2540: Cannot assign to 'compare' because it is a read-only property. + + +==== intlCollatorCompareAccessor.ts (2 errors) ==== + declare const collator: Intl.Collator; + + collator.compare("a", "b"); + ["a", "b"].sort(collator.compare); + + const compare: (this: void, x: string, y: string) => number = collator.compare; + const compareWithThis: Intl.Collator["compare"] = function (this: Intl.Collator, x: string, y: string) { + ~~~~~~~~~~~~~~~ +!!! error TS2322: Type '(this: Intl.Collator, x: string, y: string) => number' is not assignable to type '(this: void, x: string, y: string) => number'. +!!! error TS2322: The 'this' types of each signature are incompatible. +!!! error TS2322: Type 'void' is not assignable to type 'Collator'. + return 0; + }; + + collator.compare = (x: string, y: string) => 0; + ~~~~~~~ +!!! error TS2540: Cannot assign to 'compare' because it is a read-only property. + \ No newline at end of file diff --git a/tests/cases/compiler/intlCollatorCompareAccessor.ts b/tests/cases/compiler/intlCollatorCompareAccessor.ts new file mode 100644 index 0000000000000..423876cb7bb1d --- /dev/null +++ b/tests/cases/compiler/intlCollatorCompareAccessor.ts @@ -0,0 +1,15 @@ +// @strict: true +// @noEmit: true +// @noTypesAndSymbols: true + +declare const collator: Intl.Collator; + +collator.compare("a", "b"); +["a", "b"].sort(collator.compare); + +const compare: (this: void, x: string, y: string) => number = collator.compare; +const compareWithThis: Intl.Collator["compare"] = function (this: Intl.Collator, x: string, y: string) { + return 0; +}; + +collator.compare = (x: string, y: string) => 0;