Skip to content

Commit

Permalink
Fix TypeScript type for function and string array (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusU authored Jun 1, 2021
1 parent d4fe1af commit 5ac7bde
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare namespace sortOn {
type Property<T> = string | string[] | ((element: T) => unknown) | ((element: T) => unknown)[];
type Property<T> = string | ((element: T) => unknown) | (string | ((element: T) => unknown))[];
}

/**
Expand Down
1 change: 1 addition & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ expectType<any>(sortOn<any>([{x: 'b'}, {x: 'a'}, {x: 'c'}], 'x')[0].x);
expectType<string>(sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], ['x'])[0].x);
expectType<string>(sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], element => element.x)[0].x);
expectType<string>(sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], [element => element.x])[0].x);
expectType<string>(sortOn([{x: 'b', y: 1}, {x: 'a', y: 2}, {x: 'c', y: 1}], [element => element.x, 'y'])[0].x);

const property: sortOn.Property<string> = string => expectType<string>(string);
expectType<string>(sortOn(['a', 'bb', 'ccc'], property)[0]);
Expand Down
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ test('main', t => {
prop => prop.bar
])[0].bar, 2);

t.is(sortOn([
{foo: 2, bar: 1},
{foo: 1, bar: 2},
{foo: 1, bar: 3},
{foo: 3, bar: 3}
], [
prop => prop.foo,
'bar'
])[0].bar, 2);

const sorted = sortOn([
{bar: 'b'},
{foo: 'b'},
Expand Down

0 comments on commit 5ac7bde

Please sign in to comment.