Skip to content

Commit

Permalink
Merge pull request #12 from abraham/empty-classes-are-bad
Browse files Browse the repository at this point in the history
Fix empty classes acting as any
  • Loading branch information
abraham authored Jul 17, 2018
2 parents ecb9e10 + c695b5c commit 73aee9e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ import { Failure, fold, Initialized, Pending, Success } from './index';

test('Initialized', () => {
expect(new Initialized()).toBeInstanceOf(Initialized);
expect(new Initialized().kind).toEqual('Initialized');
});

test('Pending', () => {
expect(new Pending()).toBeInstanceOf(Pending);
expect(new Pending().kind).toEqual('Pending');
});

test('Success', () => {
const data = { apple: 'sauce' };
const state = new Success(data);
expect(state).toBeInstanceOf(Success);
expect(state.kind).toEqual('Success');
expect(state.data).toEqual(data);
});

Expand All @@ -23,6 +26,7 @@ test('Failure', () => {
const error = 500;
const state = new Failure(error);
expect(state).toBeInstanceOf(Failure);
expect(state.kind).toEqual('Failure');
expect(state.error).toEqual(error);
});

Expand Down
12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
export type RemoteData<D, E> = Initialized | Pending | Success<D> | Failure<E>;

export class Initialized {}
export class Initialized {
private kind = 'Initialized';
}

export class Pending {}
export class Pending {
private kind = 'Pending';
}

export class Success<D> {
private kind = 'Success';

constructor(public data: D) {
if (data === null || data === undefined) {
fail('Parameter "data" is required');
Expand All @@ -13,6 +19,8 @@ export class Success<D> {
}

export class Failure<E> {
private kind = 'Failure';

constructor(public error: E) {
if (error === null || error === undefined) {
fail('Parameter "error" is required');
Expand Down

0 comments on commit 73aee9e

Please sign in to comment.