diff --git a/types/reservoir/index.d.ts b/types/reservoir/index.d.ts new file mode 100644 index 0000000000..b08e8aebfa --- /dev/null +++ b/types/reservoir/index.d.ts @@ -0,0 +1,35 @@ +// Type definitions for reservoir 0.1 +// Project: https://github.com/imbcmdth/reservoir +// Definitions by: Dan Vanderkam +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export as namespace Reservoir; +export = Reservoir; + +/** + * Create a new reservoir sampler. + * + * @param reservoirSize is the maximum size of the reservoir. This is the number of elements + * to be randomly chosen from the input provided to it using pushSome. Default is 1. + * @param randomNumberGenerator is an optional random number generating function to use in + * place of the default Math.random. + */ +declare function Reservoir( + reservoirSize?: number, + randomNumberGenerator?: () => number +): Reservoir.ReservoirArray; // tslint:disable-line:no-unnecessary-generics + +/*~ If you want to expose types from your module as well, you can + *~ place them in this block. Often you will want to describe the + *~ shape of the return type of the function; that type should + *~ be declared in here, as this example shows. + */ +declare namespace Reservoir { + interface ReservoirArray extends Array { + /** + * datum: one or more elements to consider for inclusion into the reservoir. + * Returns the current length of the reservoir. + */ + pushSome(...datum: T[]): number; + } +} diff --git a/types/reservoir/reservoir-tests.ts b/types/reservoir/reservoir-tests.ts new file mode 100644 index 0000000000..f7775bd1bb --- /dev/null +++ b/types/reservoir/reservoir-tests.ts @@ -0,0 +1,13 @@ +import Reservoir = require('reservoir'); + +// $ExpectType ReservoirArray +const x = Reservoir(1); + +// $ExpectType number +x.pushSome(10, 20, 30, 40); + +// $ExpectType number +x[0]; + +// $ExpectType number +x.length; diff --git a/types/reservoir/tsconfig.json b/types/reservoir/tsconfig.json new file mode 100644 index 0000000000..ff04e5c9d5 --- /dev/null +++ b/types/reservoir/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "esModuleInterop": true + }, + "files": [ + "index.d.ts", + "reservoir-tests.ts" + ] +} diff --git a/types/reservoir/tslint.json b/types/reservoir/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/reservoir/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }