Skip to content

Commit 5f256fe

Browse files
author
Jayson Harshbarger
committed
fix typing issue and add typing verification
1 parent 5cd385b commit 5f256fe

File tree

8 files changed

+188
-6
lines changed

8 files changed

+188
-6
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"Randomator",
1515
"Randomators",
1616
"strs",
17+
"typedoc",
1718
"ucase",
1819
"uhexa",
1920
"Wolfowitz",

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ A more involved example is the `string` function which take several options and
6060
```js
6161
const chars$ = chars({ pool: 'abc' });
6262
const length$ = integers({ min: 3, max: 13 });
63-
const string$ = strings({ chars: chars$, length: length$ });
64-
console.log(string$()); // Prints a string between three (3) and 13 characters (inclusive) of 'a', 'b' or 'c'.
63+
const string$ = strings({ chars: chars$, length: length$ }); // A randomator that generates string between three (3) and 13 characters (inclusive) of 'a', 'b' or 'c'.
6564
```
6665

6766
And operators:

package-lock.json

Lines changed: 122 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"version": "3.0.0-1",
55
"description": "composable random generators",
66
"main": "build/main/index.js",
7-
"typings": "build/main/index.d.ts",
7+
"types": "build/main/index.d.ts",
88
"module": "build/module/index.js",
99
"repository": "https://github.com/Hypercubed/Randomator",
1010
"license": "MIT",
@@ -19,6 +19,7 @@
1919
"test": "run-s test:*",
2020
"test:lint": "eslint \"src/**/*.{js,ts}\" --ext .ts",
2121
"test:prettier": "prettier \"src/**/*.{ts,js,css,md}\" --check",
22+
"test:tsd": "tsd && echo \"Types verified\"",
2223
"test:unit": "jest --coverage --no-cache",
2324
"watch:build": "tsc -p tsconfig.main.json -w",
2425
"watch:test": "jest --watch",
@@ -66,6 +67,7 @@
6667
"prettier": "^2.5.1",
6768
"ts-jest": "^27.1.3",
6869
"ts-node": "^10.4.0",
70+
"tsd": "^0.19.1",
6971
"typedoc": "^0.22.11",
7072
"typescript": "^4.5.5"
7173
},
@@ -82,5 +84,8 @@
8284
"hooks": {
8385
"pre-push": "npm run check"
8486
}
85-
}
87+
},
88+
"tsd": {
89+
"directory": "src"
90+
}
8691
}

src/lib/randomator.test-d.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* eslint-disable @typescript-eslint/ban-types */
2+
3+
import { expectAssignable, expectNotType, expectType } from 'tsd';
4+
5+
import { Randomator } from './randomator.js';
6+
7+
// from a value
8+
const a$ = Randomator.from('a');
9+
expectAssignable<Function>(a$);
10+
expectAssignable<() => string>(a$);
11+
expectAssignable<Iterable<string>>(a$);
12+
expectType<Randomator<string>>(a$);
13+
expectType<string>(a$());
14+
15+
const b$ = Randomator.from(5);
16+
expectType<number>(b$());
17+
18+
// from a function
19+
const c$ = Randomator.from(() => 'c');
20+
expectType<string>(c$());
21+
22+
const d$ = Randomator.from(() => 5);
23+
expectType<number>(d$());
24+
25+
// constructor
26+
const e$ = new Randomator(() => Math.random());
27+
expectType<number>(e$());
28+
29+
// M(M(a)) === M(a)
30+
const f$ = new Randomator(a$);
31+
expectType<string>(f$());
32+
33+
const g$ = Randomator.from(f$);
34+
expectType<string>(g$());
35+
36+
// map
37+
const h$ = e$.map(a => String(a));
38+
expectNotType<number>(h$());
39+
expectType<string>(h$());
40+
41+
// chain
42+
const i = e$.chain(a => a + a);
43+
expectType<number>(i);
44+
45+
// filter
46+
const j$ = e$.filter(a => a > 0.5);
47+
expectType<number>(j$());
48+
49+
// toArray
50+
const k = e$.toArray(5);
51+
expectType<number[]>(k);

src/lib/randomator.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,7 @@ export class Randomator<T = unknown> extends Function implements Iterable<T> {
103103
return Array.from({ length }, () => this());
104104
}
105105
}
106+
107+
export interface Randomator<T> {
108+
(): T;
109+
}

tsconfig.main.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"noEmit": false
99
},
1010
"include": ["src/**/*.ts"],
11-
"exclude": ["src/**/*.spec.ts", "node_modules/**"]
11+
"exclude": ["src/**/*.spec.ts", "src/**/*.test-d.ts", "node_modules/**"]
1212
}

tsconfig.module.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"noEmit": false
99
},
1010
"include": ["src/**/*.ts"],
11-
"exclude": ["src/**/*.spec.ts", "node_modules/**"]
11+
"exclude": ["src/**/*.spec.ts", "src/**/*.test-d.ts", "node_modules/**"]
1212
}

0 commit comments

Comments
 (0)