From 1d2a6b1b5422c18a44579b144a2e35d530a646b4 Mon Sep 17 00:00:00 2001 From: Denis Malinochkin Date: Sat, 11 Nov 2017 21:05:13 +0300 Subject: [PATCH] feat(types): Add typings for "sort-array" package --- types/sort-array/index.d.ts | 14 +++++++++++ types/sort-array/sort-array-tests.ts | 37 ++++++++++++++++++++++++++++ types/sort-array/tsconfig.json | 23 +++++++++++++++++ types/sort-array/tslint.json | 1 + 4 files changed, 75 insertions(+) create mode 100644 types/sort-array/index.d.ts create mode 100644 types/sort-array/sort-array-tests.ts create mode 100644 types/sort-array/tsconfig.json create mode 100644 types/sort-array/tslint.json diff --git a/types/sort-array/index.d.ts b/types/sort-array/index.d.ts new file mode 100644 index 0000000000..a94ff269ff --- /dev/null +++ b/types/sort-array/index.d.ts @@ -0,0 +1,14 @@ +// Type definitions for sort-array 2.0 +// Project: https://github.com/75lb/sort-array#readme +// Definitions by: mrmlnc +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +type ColumnNames = keyof T | Array; +type CustomOrderTypes = Array; +type CustomOrder = Record>; + +declare function sortArray(recordset: T[], columnNames: ColumnNames, customOrder?: Partial>): T[]; +declare function sortArray(recordset: object[], columnNames: string | string[], customOrder?: Record): object[]; + +export = sortArray; diff --git a/types/sort-array/sort-array-tests.ts b/types/sort-array/sort-array-tests.ts new file mode 100644 index 0000000000..cc33e5600d --- /dev/null +++ b/types/sort-array/sort-array-tests.ts @@ -0,0 +1,37 @@ +import sort = require('sort-array'); + +interface AlphabetItem { + letter: string; + position: number; +} + +const alphabet: AlphabetItem[] = [ + { letter: 'b', position: 2 }, + { letter: 'a', position: 1 }, + { letter: 'c', position: 3 } +]; + +// $ExpectType AlphabetItem[] +sort(alphabet, 'letter'); +sort(alphabet, ['letter']); +sort(alphabet, 'letter'); + +sort(alphabet, 'letter', { letter: ['c', 'a'] }); + +// Allow to use string and number, because we cannot get type of the current property +sort(alphabet, 'letter', { letter: ['c', 'a', 1] }); + +interface Input { + inner: { number: number }; +} + +const input: Input[] = [ + { inner: { number: 5 } }, + { inner: { number: 3 } } +]; + +// Allow to use without types + +// $ExpectType object[] +sort(input, 'inner.number'); +sort(input, 'inner.number', { 'inner.number': [5, 3] }); diff --git a/types/sort-array/tsconfig.json b/types/sort-array/tsconfig.json new file mode 100644 index 0000000000..935747c5e6 --- /dev/null +++ b/types/sort-array/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "sort-array-tests.ts" + ] +} diff --git a/types/sort-array/tslint.json b/types/sort-array/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/sort-array/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }