add types for weighted-random-object (#36885)

This commit is contained in:
mike castleman 2019-07-15 17:32:04 -04:00 committed by Andrew Branch
parent fc8781c262
commit 670e84438c
4 changed files with 51 additions and 0 deletions

14
types/weighted-random-object/index.d.ts vendored Normal file
View File

@ -0,0 +1,14 @@
// Type definitions for weighted-random-object 1.0
// Project: https://github.com/misund/weighted-random-object
// Definitions by: mike castleman <https://github.com/mlc>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.7
interface Weighted {
weight: number;
}
declare function weightedRandomObject<T extends Weighted>(objects: ReadonlyArray<T>): T;
export = weightedRandomObject;

View File

@ -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",
"weighted-random-object-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

View File

@ -0,0 +1,12 @@
import weightedRandomObject from 'weighted-random-object';
interface MyObject {
data: string;
weight: number;
}
const objs: MyObject[] = [{ data: 'a', weight: 7 }, { data: 'b', weight: 5 }];
weightedRandomObject(objs); // $ExpectType MyObject
weightedRandomObject([{ f: 5, weight: 1 }, { f: 9, weight: 2 }]).f; // $ExpectType number