Skip to content

Commit be55d41

Browse files
authored
Merge pull request #431 from abraham/guards
Add type guards
2 parents e66b71f + 716b9c2 commit be55d41

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/index.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
import { Failure, fold, Initialized, Kinds, Pending, Success } from './index';
1+
import {
2+
Failure,
3+
fold,
4+
Initialized,
5+
isFailure,
6+
isInitialized,
7+
isPending,
8+
isSuccess,
9+
Kinds,
10+
Pending,
11+
Success,
12+
} from './index';
213

314
test('Kinds', () => {
415
expect(Kinds).toEqual({
@@ -12,11 +23,15 @@ test('Kinds', () => {
1223
test('Initialized', () => {
1324
expect(new Initialized()).toBeInstanceOf(Initialized);
1425
expect(new Initialized().kind).toEqual('Initialized');
26+
expect(isInitialized(new Initialized())).toBeTruthy();
27+
expect(isInitialized(new Pending())).toBeFalsy();
1528
});
1629

1730
test('Pending', () => {
1831
expect(new Pending()).toBeInstanceOf(Pending);
1932
expect(new Pending().kind).toEqual('Pending');
33+
expect(isPending(new Pending())).toBeTruthy();
34+
expect(isPending(new Initialized())).toBeFalsy();
2035
});
2136

2237
test('Success', () => {
@@ -25,10 +40,16 @@ test('Success', () => {
2540
expect(state).toBeInstanceOf(Success);
2641
expect(state.kind).toEqual('Success');
2742
expect(state.data).toEqual(data);
43+
expect(isSuccess(state)).toBeTruthy();
44+
expect(isSuccess(new Initialized())).toBeFalsy();
2845
});
2946

3047
test('Success without data', () => {
3148
expect(() => new Success()).toThrowError('Parameter "data" is required');
49+
expect(() => new Success(null)).toThrowError('Parameter "data" is required');
50+
expect(() => new Success(undefined)).toThrowError(
51+
'Parameter "data" is required',
52+
);
3253
});
3354

3455
test('Failure', () => {
@@ -37,10 +58,16 @@ test('Failure', () => {
3758
expect(state).toBeInstanceOf(Failure);
3859
expect(state.kind).toEqual('Failure');
3960
expect(state.error).toEqual(error);
61+
expect(isFailure(state)).toBeTruthy();
62+
expect(isFailure(new Initialized())).toBeFalsy();
4063
});
4164

4265
test('Failure without error', () => {
4366
expect(() => new Failure()).toThrowError('Parameter "error" is required');
67+
expect(() => new Failure(null)).toThrowError('Parameter "error" is required');
68+
expect(() => new Failure(undefined)).toThrowError(
69+
'Parameter "error" is required',
70+
);
4471
});
4572

4673
test('fold initialized', () => {

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,12 @@ export function fold<T, E, D>(
6262
}
6363
};
6464
}
65+
66+
export const isInitialized = (state: unknown): state is Initialized =>
67+
state instanceof Initialized;
68+
export const isPending = (state: unknown): state is Pending =>
69+
state instanceof Pending;
70+
export const isFailure = <E>(state: unknown): state is Failure<E> =>
71+
state instanceof Failure;
72+
export const isSuccess = <D>(state: unknown): state is Success<D> =>
73+
state instanceof Success;

0 commit comments

Comments
 (0)