Skip to content

Commit 701fffc

Browse files
committed
feat!: switch order of generic input types
This commit also fixes the import path for the `Collection` type definition. BREAKING CHANGE: switch order of generic input types To migrate, users should update their type definitions accordingly. Ref: stdlib-js@bde4671
1 parent ed797c1 commit 701fffc

2 files changed

Lines changed: 52 additions & 20 deletions

File tree

lib/node_modules/@stdlib/utils/key-by/docs/types/index.d.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,30 @@
2020

2121
/// <reference types="@stdlib/types"/>
2222

23-
import { Collection } from '@stdlib/types/object';
23+
import { Collection } from '@stdlib/types/array';
2424

2525
/**
26-
* Function invoked for each collection element returning a key.
26+
* Object key.
27+
*/
28+
type Key = string | symbol | number;
29+
30+
/**
31+
* Function which returns a key for each collection element.
2732
*
2833
* @param value - collection value
2934
*/
30-
type Unary<T, V> = ( this: T, value: V ) => string;
35+
type Unary<T, V> = ( this: V, value: T ) => Key;
3136

3237
/**
33-
* Function invoked for each collection element returning a key.
38+
* Function which returns a key for each collection element.
3439
*
3540
* @param value - collection value
3641
* @param index - collection index
3742
*/
38-
type Binary<T, V> = ( this: T, value: V, index: number ) => string;
43+
type Binary<T, V> = ( this: V, value: T, index: number ) => Key;
3944

4045
/**
41-
* Function invoked for each collection element returning a key.
46+
* Function which returns a key for each collection element.
4247
*
4348
* @param value - collection value
4449
* @param index - collection index
@@ -78,7 +83,7 @@ type Callback<T, V> = Unary<T, V> | Binary<T, V>;
7883
* var obj = keyBy( collection, toKey );
7984
* // returns { 'beep': { 'name': 'beep', 'a': 1 }, 'boop': { 'name': 'boop', 'b': 2 } }
8085
*/
81-
declare function keyBy<T, V>( collection: Collection<V>, fcn: Callback<T, V>, thisArg?: ThisParameterType<Callback<T, V>> ): Record<string, V>; // tslint-disable-line max-line-length
86+
declare function keyBy<T = unknown, V = unknown>( collection: Collection<T>, fcn: Callback<T, V>, thisArg?: ThisParameterType<Callback<T, V>> ): Record<Key, T>;
8287

8388

8489
// EXPORTS //

lib/node_modules/@stdlib/utils/key-by/docs/types/test.ts

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import keyBy = require( './index' );
2020

21+
/**
22+
* Dummy interface.
23+
*/
2124
interface Foo {
2225
/**
2326
* String indicating the name.
@@ -35,10 +38,19 @@ interface Foo {
3538
b?: number;
3639
}
3740

38-
const toKey = ( value: Foo ) => {
41+
/**
42+
* Dummy function.
43+
*
44+
* @param value - input value
45+
* @returns key
46+
*/
47+
function toKey( value: Foo ): string {
3948
return value.name;
40-
};
49+
}
4150

51+
/**
52+
* Dummy object.
53+
*/
4254
const funcs = {
4355
'count': 0,
4456
'toKey': function toKey( this: { count: number; }, value: Foo ): string {
@@ -52,9 +64,14 @@ const funcs = {
5264

5365
// The function returns an object...
5466
{
55-
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], toKey ); // $ExpectType Record<string, { name: string; a: number; b?: undefined; } | { name: string; b: number; a?: undefined; }>
56-
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], toKey, {} ); // $ExpectType Record<string, { name: string; a: number; b?: undefined; } | { name: string; b: number; a?: undefined; }>
57-
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], funcs.toKey, { 'count': 0 } ); // $ExpectType Record<string, { name: string; a: number; b?: undefined; } | { name: string; b: number; a?: undefined; }>
67+
const arr = [
68+
{ 'name': 'beep', 'a': 1 },
69+
{ 'name': 'boop', 'b': 2 }
70+
];
71+
72+
keyBy( arr, toKey ); // $ExpectType Record<Key, { name: string; a: number; b?: undefined; } | { name: string; b: number; a?: undefined; }>
73+
keyBy( arr, toKey, {} ); // $ExpectType Record<Key, { name: string; a: number; b?: undefined; } | { name: string; b: number; a?: undefined; }>
74+
keyBy( arr, funcs.toKey, { 'count': 0 } ); // $ExpectType Record<Key, { name: string; a: number; b?: undefined; } | { name: string; b: number; a?: undefined; }>
5875
}
5976

6077
// The compiler throws an error if the function is provided a first argument which is not a collection...
@@ -67,17 +84,27 @@ const funcs = {
6784

6885
// The compiler throws an error if the function is provided a second argument which is not a function...
6986
{
70-
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], 2 ); // $ExpectError
71-
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], false ); // $ExpectError
72-
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], true ); // $ExpectError
73-
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], 'abc' ); // $ExpectError
74-
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], {} ); // $ExpectError
75-
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ], [] ); // $ExpectError
87+
const arr = [
88+
{ 'name': 'beep', 'a': 1 },
89+
{ 'name': 'boop', 'b': 2 }
90+
];
91+
92+
keyBy( arr, 2 ); // $ExpectError
93+
keyBy( arr, false ); // $ExpectError
94+
keyBy( arr, true ); // $ExpectError
95+
keyBy( arr, 'abc' ); // $ExpectError
96+
keyBy( arr, {} ); // $ExpectError
97+
keyBy( arr, [] ); // $ExpectError
7698
}
7799

78100
// The compiler throws an error if the function is provided an invalid number of arguments...
79101
{
102+
const arr = [
103+
{ 'name': 'beep', 'a': 1 },
104+
{ 'name': 'boop', 'b': 2 }
105+
];
106+
80107
keyBy(); // $ExpectError
81-
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ] ); // $ExpectError
82-
keyBy( [ { 'name': 'beep', 'a': 1 }, { 'name': 'boop', 'b': 2 } ]], toKey, {}, 3 ); // $ExpectError
108+
keyBy( arr ); // $ExpectError
109+
keyBy( arr, toKey, {}, 3 ); // $ExpectError
83110
}

0 commit comments

Comments
 (0)