Skip to content

fix(Intl): make Intl.Collator.compare a getter that returns a bound function (#62048) #62052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4419,7 +4419,7 @@ declare namespace Intl {
}

interface Collator {
compare(x: string, y: string): number;
compare(this: void, x: string, y: string): number;
resolvedOptions(): ResolvedCollatorOptions;
}

Expand Down
44 changes: 44 additions & 0 deletions tests/baselines/reference/collatorCompareBoundFunction.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
error TS2318: Cannot find global type 'Array'.
error TS2318: Cannot find global type 'Boolean'.
error TS2318: Cannot find global type 'CallableFunction'.
error TS2318: Cannot find global type 'Function'.
error TS2318: Cannot find global type 'IArguments'.
error TS2318: Cannot find global type 'NewableFunction'.
error TS2318: Cannot find global type 'Number'.
error TS2318: Cannot find global type 'Object'.
error TS2318: Cannot find global type 'RegExp'.
error TS2318: Cannot find global type 'String'.
collatorCompareBoundFunction.ts(1,27): error TS2339: Property 'Collator' does not exist on type 'typeof Intl'.
collatorCompareBoundFunction.ts(14,32): error TS2339: Property 'sort' does not exist on type '{}'.


!!! error TS2318: Cannot find global type 'Array'.
!!! error TS2318: Cannot find global type 'Boolean'.
!!! error TS2318: Cannot find global type 'CallableFunction'.
!!! error TS2318: Cannot find global type 'Function'.
!!! error TS2318: Cannot find global type 'IArguments'.
!!! error TS2318: Cannot find global type 'NewableFunction'.
!!! error TS2318: Cannot find global type 'Number'.
!!! error TS2318: Cannot find global type 'Object'.
!!! error TS2318: Cannot find global type 'RegExp'.
!!! error TS2318: Cannot find global type 'String'.
==== collatorCompareBoundFunction.ts (2 errors) ====
const collator = new Intl.Collator();
~~~~~~~~
!!! error TS2339: Property 'Collator' does not exist on type 'typeof Intl'.

// Should be usable as a function with no dynamic `this`
const cmp: (a: string, b: string) => number = collator.compare;

// Function expecting a `this: void` bound compare function
function useCompare(fn: (this: void, a: string, b: string) => number): number {
return fn("a", "b");
}

useCompare(collator.compare);

// Should work with Array.prototype.sort
const sorted = ["z", "ä", "a"].sort(collator.compare);
~~~~
!!! error TS2339: Property 'sort' does not exist on type '{}'.

31 changes: 31 additions & 0 deletions tests/baselines/reference/collatorCompareBoundFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [tests/cases/conformance/intl/collatorCompareBoundFunction.ts] ////

//// [collatorCompareBoundFunction.ts]
const collator = new Intl.Collator();

// Should be usable as a function with no dynamic `this`
const cmp: (a: string, b: string) => number = collator.compare;

// Function expecting a `this: void` bound compare function
function useCompare(fn: (this: void, a: string, b: string) => number): number {
return fn("a", "b");
}

useCompare(collator.compare);

// Should work with Array.prototype.sort
const sorted = ["z", "ä", "a"].sort(collator.compare);


//// [collatorCompareBoundFunction.js]
"use strict";
const collator = new Intl.Collator();
// Should be usable as a function with no dynamic `this`
const cmp = collator.compare;
// Function expecting a `this: void` bound compare function
function useCompare(fn) {
return fn("a", "b");
}
useCompare(collator.compare);
// Should work with Array.prototype.sort
const sorted = ["z", "ä", "a"].sort(collator.compare);
35 changes: 35 additions & 0 deletions tests/baselines/reference/collatorCompareBoundFunction.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//// [tests/cases/conformance/intl/collatorCompareBoundFunction.ts] ////

=== collatorCompareBoundFunction.ts ===
const collator = new Intl.Collator();
>collator : Symbol(collator, Decl(collatorCompareBoundFunction.ts, 0, 5))
>Intl : Symbol(Intl, Decl(lib.es2022.intl.d.ts, --, --))

// Should be usable as a function with no dynamic `this`
const cmp: (a: string, b: string) => number = collator.compare;
>cmp : Symbol(cmp, Decl(collatorCompareBoundFunction.ts, 3, 5))
>a : Symbol(a, Decl(collatorCompareBoundFunction.ts, 3, 12))
>b : Symbol(b, Decl(collatorCompareBoundFunction.ts, 3, 22))
>collator : Symbol(collator, Decl(collatorCompareBoundFunction.ts, 0, 5))

// Function expecting a `this: void` bound compare function
function useCompare(fn: (this: void, a: string, b: string) => number): number {
>useCompare : Symbol(useCompare, Decl(collatorCompareBoundFunction.ts, 3, 63))
>fn : Symbol(fn, Decl(collatorCompareBoundFunction.ts, 6, 20))
>this : Symbol(this, Decl(collatorCompareBoundFunction.ts, 6, 25))
>a : Symbol(a, Decl(collatorCompareBoundFunction.ts, 6, 36))
>b : Symbol(b, Decl(collatorCompareBoundFunction.ts, 6, 47))

return fn("a", "b");
>fn : Symbol(fn, Decl(collatorCompareBoundFunction.ts, 6, 20))
}

useCompare(collator.compare);
>useCompare : Symbol(useCompare, Decl(collatorCompareBoundFunction.ts, 3, 63))
>collator : Symbol(collator, Decl(collatorCompareBoundFunction.ts, 0, 5))

// Should work with Array.prototype.sort
const sorted = ["z", "ä", "a"].sort(collator.compare);
>sorted : Symbol(sorted, Decl(collatorCompareBoundFunction.ts, 13, 5))
>collator : Symbol(collator, Decl(collatorCompareBoundFunction.ts, 0, 5))

91 changes: 91 additions & 0 deletions tests/baselines/reference/collatorCompareBoundFunction.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//// [tests/cases/conformance/intl/collatorCompareBoundFunction.ts] ////

=== collatorCompareBoundFunction.ts ===
const collator = new Intl.Collator();
>collator : any
> : ^^^
>new Intl.Collator() : any
> : ^^^
>Intl.Collator : any
> : ^^^
>Intl : typeof Intl
> : ^^^^^^^^^^^
>Collator : any
> : ^^^

// Should be usable as a function with no dynamic `this`
const cmp: (a: string, b: string) => number = collator.compare;
>cmp : (a: string, b: string) => number
> : ^ ^^ ^^ ^^ ^^^^^
>a : string
> : ^^^^^^
>b : string
> : ^^^^^^
>collator.compare : any
> : ^^^
>collator : any
> : ^^^
>compare : any
> : ^^^

// Function expecting a `this: void` bound compare function
function useCompare(fn: (this: void, a: string, b: string) => number): number {
>useCompare : (fn: (this: void, a: string, b: string) => number) => number
> : ^ ^^ ^^^^^
>fn : (this: void, a: string, b: string) => number
> : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^
>this : void
> : ^^^^
>a : string
> : ^^^^^^
>b : string
> : ^^^^^^

return fn("a", "b");
>fn("a", "b") : number
> : ^^^^^^
>fn : (this: void, a: string, b: string) => number
> : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^
>"a" : "a"
> : ^^^
>"b" : "b"
> : ^^^
}

useCompare(collator.compare);
>useCompare(collator.compare) : number
> : ^^^^^^
>useCompare : (fn: (this: void, a: string, b: string) => number) => number
> : ^ ^^ ^^^^^
>collator.compare : any
> : ^^^
>collator : any
> : ^^^
>compare : any
> : ^^^

// Should work with Array.prototype.sort
const sorted = ["z", "ä", "a"].sort(collator.compare);
>sorted : any
> : ^^^
>["z", "ä", "a"].sort(collator.compare) : any
> : ^^^
>["z", "ä", "a"].sort : any
> : ^^^
>["z", "ä", "a"] : {}
> : ^^
>"z" : "z"
> : ^^^
>"ä" : "ä"
> : ^^^
>"a" : "a"
> : ^^^
>sort : any
> : ^^^
>collator.compare : any
> : ^^^
>collator : any
> : ^^^
>compare : any
> : ^^^

18 changes: 18 additions & 0 deletions tests/cases/conformance/intl/collatorCompareBoundFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @target: es2022
// @lib: es2022.intl
// @strict: true

const collator = new Intl.Collator();

// Should be usable as a function with no dynamic `this`
const cmp: (a: string, b: string) => number = collator.compare;

// Function expecting a `this: void` bound compare function
function useCompare(fn: (this: void, a: string, b: string) => number): number {
return fn("a", "b");
}

useCompare(collator.compare);

// Should work with Array.prototype.sort
const sorted = ["z", "ä", "a"].sort(collator.compare);