Skip to content

Commit 005feeb

Browse files
authored
Add type tests using Vitest (and fix any issues found along the way) (#32)
* add type test setup * fix window event map * add catch type test and fix typing * add type predicate overload to .find * tests for catch, constructor, drop, every, finally, foreach, some, take * tests for filter, find, first, flatMap, forEach, inspect, last, map, switchMap, toArray * test for reduce, and add overloads * tests for from, takeUntil * add type tests to CI * run type tests before publish * .catch must return a valid ObservableInput * add some more tests for ObservableInput accepting methods * remove unused import * enforce use of npm
1 parent 17cdcc7 commit 005feeb

30 files changed

Lines changed: 1777 additions & 19 deletions

.github/workflows/publish.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
- run: npm install -g npm@latest
2222
- run: npm i
2323
- run: npm test
24+
- run: npm run test:types
2425
- run: npm run minify
2526
- run: npm version ${TAG_NAME} --git-tag-version=false
2627
env:

.github/workflows/test.yml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ jobs:
1010
build:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v4
14-
- name: Use Node.js 22.x
15-
uses: actions/setup-node@v4
16-
with:
17-
node-version: 22.x
18-
- name: npm install, build, and test
19-
run: |
20-
npm it
21-
env:
22-
CI: true
13+
- uses: actions/checkout@v4
14+
- name: Use Node.js 22.x
15+
uses: actions/setup-node@v4
16+
with:
17+
node-version: 22.x
18+
- name: npm install, build, and test
19+
run: |
20+
npm it
21+
env:
22+
CI: true
23+
24+
- name: test types
25+
run: npm run test:types

index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ declare global {
5353
}
5454

5555
interface Window {
56-
when<T extends keyof HTMLElementEventMap>(
56+
when<T extends keyof WindowEventMap>(
5757
event: T,
5858
options?: ObservableEventListenerOptions
59-
): Observable<HTMLElementEventMap[T]>;
59+
): Observable<WindowEventMap[T]>;
6060
when(
6161
type: string,
6262
options?: ObservableEventListenerOptions

observable.d.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export type Reducer<T = any, R = any> = (
4949
) => R;
5050
export type Mapper<T = any, R = any> = (value: T, index: number) => R;
5151
export type Visitor<T = any> = (value: T, index: number) => void;
52-
export type CatchCallback<R = any> = (value: any) => Observable<R> | R;
52+
export type CatchCallback<R = any> = (value: any) => ObservableInput<R>;
5353

5454
export type ObservableInput<T> =
5555
| Observable<T>
@@ -70,21 +70,35 @@ export class Observable<T = any> {
7070
flatMap<R>(mapper: Mapper<T, ObservableInput<R>>): Observable<R>;
7171
switchMap<R>(mapper: Mapper<T, ObservableInput<R>>): Observable<R>;
7272
inspect(inspectorUnion?: ObservableInspectorUnion<T>): Observable<T>;
73-
catch(callback: CatchCallback): Observable<T>;
73+
catch<R>(callback: CatchCallback<R>): Observable<T | R>;
7474
finally(callback: () => void): Observable<T>;
7575
toArray(options?: SubscribeOptions): Promise<T[]>;
7676
forEach(callback: Visitor<T>, options?: SubscribeOptions): Promise<void>;
7777
every(predicate: Predicate<T>, options?: SubscribeOptions): Promise<boolean>;
7878
first(options?: SubscribeOptions): Promise<T>;
7979
last(options?: SubscribeOptions): Promise<T>;
80+
find<U extends T>(
81+
predicate: TypePredicate<T, U>,
82+
options?: SubscribeOptions
83+
): Promise<U | undefined>;
8084
find(
8185
predicate: Predicate<T>,
8286
options?: SubscribeOptions
8387
): Promise<T | undefined>;
8488
some(predicate: Predicate<T>, options?: SubscribeOptions): Promise<boolean>;
89+
reduce(
90+
reducer: Reducer<T, T>,
91+
initialValue?: undefined,
92+
options?: SubscribeOptions
93+
): Promise<T | undefined>;
94+
reduce(
95+
reducer: Reducer<T, T>,
96+
initialValue: T,
97+
options?: SubscribeOptions
98+
): Promise<T>;
8599
reduce<R>(
86100
reducer: Reducer<T, R>,
87-
initialValue?: R,
101+
initialValue: R,
88102
options?: SubscribeOptions
89103
): Promise<R>;
90104
}

0 commit comments

Comments
 (0)