Skip to content

Commit 4ee251b

Browse files
author
Orta
authored
Merge pull request microsoft#33372 from m1gu3l/patch-1
Better Array.sort compareFn docs
2 parents 91be368 + b544901 commit 4ee251b

7 files changed

+68
-27
lines changed

src/lib/es5.d.ts

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,12 @@ interface Array<T> {
12351235
slice(start?: number, end?: number): T[];
12361236
/**
12371237
* Sorts an array.
1238-
* @param compareFn The name of the function used to determine the order of the elements. If omitted, the elements are sorted in ascending, ASCII character order.
1238+
* @param compareFn Function used to determine the order of the elements. It is expected to return
1239+
* a negative value if first argument is less than second argument, zero if they're equal and a positive
1240+
* value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
1241+
* ```ts
1242+
* [11,2,22,1].sort((a, b) => a - b)
1243+
* ```
12391244
*/
12401245
sort(compareFn?: (a: T, b: T) => number): this;
12411246
/**
@@ -1876,8 +1881,12 @@ interface Int8Array {
18761881

18771882
/**
18781883
* Sorts an array.
1879-
* @param compareFn The name of the function used to determine the order of the elements. If
1880-
* omitted, the elements are sorted in ascending, ASCII character order.
1884+
* @param compareFn Function used to determine the order of the elements. It is expected to return
1885+
* a negative value if first argument is less than second argument, zero if they're equal and a positive
1886+
* value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
1887+
* ```ts
1888+
* [11,2,22,1].sort((a, b) => a - b)
1889+
* ```
18811890
*/
18821891
sort(compareFn?: (a: number, b: number) => number): this;
18831892

@@ -2151,8 +2160,12 @@ interface Uint8Array {
21512160

21522161
/**
21532162
* Sorts an array.
2154-
* @param compareFn The name of the function used to determine the order of the elements. If
2155-
* omitted, the elements are sorted in ascending, ASCII character order.
2163+
* @param compareFn Function used to determine the order of the elements. It is expected to return
2164+
* a negative value if first argument is less than second argument, zero if they're equal and a positive
2165+
* value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
2166+
* ```ts
2167+
* [11,2,22,1].sort((a, b) => a - b)
2168+
* ```
21562169
*/
21572170
sort(compareFn?: (a: number, b: number) => number): this;
21582171

@@ -2426,8 +2439,12 @@ interface Uint8ClampedArray {
24262439

24272440
/**
24282441
* Sorts an array.
2429-
* @param compareFn The name of the function used to determine the order of the elements. If
2430-
* omitted, the elements are sorted in ascending, ASCII character order.
2442+
* @param compareFn Function used to determine the order of the elements. It is expected to return
2443+
* a negative value if first argument is less than second argument, zero if they're equal and a positive
2444+
* value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
2445+
* ```ts
2446+
* [11,2,22,1].sort((a, b) => a - b)
2447+
* ```
24312448
*/
24322449
sort(compareFn?: (a: number, b: number) => number): this;
24332450

@@ -2699,8 +2716,12 @@ interface Int16Array {
26992716

27002717
/**
27012718
* Sorts an array.
2702-
* @param compareFn The name of the function used to determine the order of the elements. If
2703-
* omitted, the elements are sorted in ascending, ASCII character order.
2719+
* @param compareFn Function used to determine the order of the elements. It is expected to return
2720+
* a negative value if first argument is less than second argument, zero if they're equal and a positive
2721+
* value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
2722+
* ```ts
2723+
* [11,2,22,1].sort((a, b) => a - b)
2724+
* ```
27042725
*/
27052726
sort(compareFn?: (a: number, b: number) => number): this;
27062727

@@ -2975,8 +2996,12 @@ interface Uint16Array {
29752996

29762997
/**
29772998
* Sorts an array.
2978-
* @param compareFn The name of the function used to determine the order of the elements. If
2979-
* omitted, the elements are sorted in ascending, ASCII character order.
2999+
* @param compareFn Function used to determine the order of the elements. It is expected to return
3000+
* a negative value if first argument is less than second argument, zero if they're equal and a positive
3001+
* value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
3002+
* ```ts
3003+
* [11,2,22,1].sort((a, b) => a - b)
3004+
* ```
29803005
*/
29813006
sort(compareFn?: (a: number, b: number) => number): this;
29823007

@@ -3250,8 +3275,12 @@ interface Int32Array {
32503275

32513276
/**
32523277
* Sorts an array.
3253-
* @param compareFn The name of the function used to determine the order of the elements. If
3254-
* omitted, the elements are sorted in ascending, ASCII character order.
3278+
* @param compareFn Function used to determine the order of the elements. It is expected to return
3279+
* a negative value if first argument is less than second argument, zero if they're equal and a positive
3280+
* value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
3281+
* ```ts
3282+
* [11,2,22,1].sort((a, b) => a - b)
3283+
* ```
32553284
*/
32563285
sort(compareFn?: (a: number, b: number) => number): this;
32573286

@@ -3524,8 +3553,12 @@ interface Uint32Array {
35243553

35253554
/**
35263555
* Sorts an array.
3527-
* @param compareFn The name of the function used to determine the order of the elements. If
3528-
* omitted, the elements are sorted in ascending, ASCII character order.
3556+
* @param compareFn Function used to determine the order of the elements. It is expected to return
3557+
* a negative value if first argument is less than second argument, zero if they're equal and a positive
3558+
* value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
3559+
* ```ts
3560+
* [11,2,22,1].sort((a, b) => a - b)
3561+
* ```
35293562
*/
35303563
sort(compareFn?: (a: number, b: number) => number): this;
35313564

@@ -3799,8 +3832,12 @@ interface Float32Array {
37993832

38003833
/**
38013834
* Sorts an array.
3802-
* @param compareFn The name of the function used to determine the order of the elements. If
3803-
* omitted, the elements are sorted in ascending, ASCII character order.
3835+
* @param compareFn Function used to determine the order of the elements. It is expected to return
3836+
* a negative value if first argument is less than second argument, zero if they're equal and a positive
3837+
* value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
3838+
* ```ts
3839+
* [11,2,22,1].sort((a, b) => a - b)
3840+
* ```
38043841
*/
38053842
sort(compareFn?: (a: number, b: number) => number): this;
38063843

@@ -4075,8 +4112,12 @@ interface Float64Array {
40754112

40764113
/**
40774114
* Sorts an array.
4078-
* @param compareFn The name of the function used to determine the order of the elements. If
4079-
* omitted, the elements are sorted in ascending, ASCII character order.
4115+
* @param compareFn Function used to determine the order of the elements. It is expected to return
4116+
* a negative value if first argument is less than second argument, zero if they're equal and a positive
4117+
* value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
4118+
* ```ts
4119+
* [11,2,22,1].sort((a, b) => a - b)
4120+
* ```
40804121
*/
40814122
sort(compareFn?: (a: number, b: number) => number): this;
40824123

tests/baselines/reference/destructuringParameterDeclaration4.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(
4141
a1(...array2); // Error parameter type is (number|string)[]
4242
~~~~~~
4343
!!! error TS2552: Cannot find name 'array2'. Did you mean 'Array'?
44-
!!! related TS2728 /.ts/lib.es5.d.ts:1368:13: 'Array' is declared here.
44+
!!! related TS2728 /.ts/lib.es5.d.ts:1373:13: 'Array' is declared here.
4545
a5([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]]
4646
~~~~~~~~
4747
!!! error TS2322: Type 'string' is not assignable to type '[[any]]'.

tests/baselines/reference/promisePermutations.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ tests/cases/compiler/promisePermutations.ts(160,21): error TS2769: No overload m
447447
!!! error TS2769: The last overload gave the following error.
448448
!!! error TS2769: Argument of type '(x: any) => IPromise<string>' is not assignable to parameter of type '(error: any) => Promise<number>'.
449449
!!! error TS2769: Property 'catch' is missing in type 'IPromise<string>' but required in type 'Promise<number>'.
450-
!!! related TS2728 /.ts/lib.es5.d.ts:1413:5: 'catch' is declared here.
450+
!!! related TS2728 /.ts/lib.es5.d.ts:1418:5: 'catch' is declared here.
451451
!!! related TS2771 tests/cases/compiler/promisePermutations.ts:5:5: The last overload is declared here.
452452
var s10g = s10.then(testFunctionP, nIPromise, sIPromise).then(sPromise, sIPromise, sIPromise); // ok
453453

tests/baselines/reference/promisePermutations2.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ tests/cases/compiler/promisePermutations2.ts(159,21): error TS2345: Argument of
351351
~~~~~~~~~
352352
!!! error TS2345: Argument of type '(x: any) => IPromise<string>' is not assignable to parameter of type '(error: any) => Promise<number>'.
353353
!!! error TS2345: Property 'catch' is missing in type 'IPromise<string>' but required in type 'Promise<number>'.
354-
!!! related TS2728 /.ts/lib.es5.d.ts:1413:5: 'catch' is declared here.
354+
!!! related TS2728 /.ts/lib.es5.d.ts:1418:5: 'catch' is declared here.
355355
var s10g = s10.then(testFunctionP, nIPromise, sIPromise).then(sPromise, sIPromise, sIPromise); // ok
356356

357357
var r11: IPromise<number>;

tests/baselines/reference/promisePermutations3.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of
398398
!!! error TS2769: The last overload gave the following error.
399399
!!! error TS2769: Argument of type '(x: any) => IPromise<string>' is not assignable to parameter of type '(error: any) => Promise<number>'.
400400
!!! error TS2769: Property 'catch' is missing in type 'IPromise<string>' but required in type 'Promise<number>'.
401-
!!! related TS2728 /.ts/lib.es5.d.ts:1413:5: 'catch' is declared here.
401+
!!! related TS2728 /.ts/lib.es5.d.ts:1418:5: 'catch' is declared here.
402402
!!! related TS2771 tests/cases/compiler/promisePermutations3.ts:7:5: The last overload is declared here.
403403
var s10g = s10.then(testFunctionP, nIPromise, sIPromise).then(sPromise, sIPromise, sIPromise); // ok
404404

@@ -445,5 +445,5 @@ tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of
445445
~~~~~~~~~~~~~~~
446446
!!! error TS2345: Argument of type '{ <T>(x: T): IPromise<T>; <T>(x: T, y: T): Promise<T>; }' is not assignable to parameter of type '(value: (x: any) => any) => Promise<unknown>'.
447447
!!! error TS2345: Property 'catch' is missing in type 'IPromise<any>' but required in type 'Promise<unknown>'.
448-
!!! related TS2728 /.ts/lib.es5.d.ts:1413:5: 'catch' is declared here.
448+
!!! related TS2728 /.ts/lib.es5.d.ts:1418:5: 'catch' is declared here.
449449
var s12c = s12.then(testFunction12P, testFunction12, testFunction12); // ok

tests/baselines/reference/promiseTypeInference.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ tests/cases/compiler/promiseTypeInference.ts(10,39): error TS2769: No overload m
3232
!!! error TS2769: Types of parameters 'success' and 'onfulfilled' are incompatible.
3333
!!! error TS2769: Type 'TResult1 | PromiseLike<TResult1>' is not assignable to type 'IPromise<TResult1 | TResult2>'.
3434
!!! error TS2769: Type 'TResult1' is not assignable to type 'IPromise<TResult1 | TResult2>'.
35-
!!! related TS2728 /.ts/lib.es5.d.ts:1413:5: 'catch' is declared here.
35+
!!! related TS2728 /.ts/lib.es5.d.ts:1418:5: 'catch' is declared here.
3636
!!! related TS6502 tests/cases/compiler/promiseTypeInference.ts:2:23: The expected type comes from the return type of this signature.
37-
!!! related TS6502 /.ts/lib.es5.d.ts:1406:57: The expected type comes from the return type of this signature.
37+
!!! related TS6502 /.ts/lib.es5.d.ts:1411:57: The expected type comes from the return type of this signature.
3838

tests/baselines/reference/redefineArray.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ tests/cases/compiler/redefineArray.ts(1,1): error TS2741: Property 'isArray' is
55
Array = function (n:number, s:string) {return n;};
66
~~~~~
77
!!! error TS2741: Property 'isArray' is missing in type '(n: number, s: string) => number' but required in type 'ArrayConstructor'.
8-
!!! related TS2728 /.ts/lib.es5.d.ts:1364:5: 'isArray' is declared here.
8+
!!! related TS2728 /.ts/lib.es5.d.ts:1369:5: 'isArray' is declared here.

0 commit comments

Comments
 (0)