Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(typings): Adds type definitions for ActionsObservable.from/of
  • Loading branch information
jayphelps committed Nov 17, 2016
1 parent fd393a1 commit 0cba557
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
21 changes: 20 additions & 1 deletion index.d.ts
@@ -1,8 +1,27 @@
import { Middleware, MiddlewareAPI } from 'redux';
import { Observable } from 'rxjs/Observable';
import { Observable, ObservableInput } from 'rxjs/Observable';
import { Scheduler } from 'rxjs/Scheduler';
import { Operator } from 'rxjs/Operator';

export declare class ActionsObservable<T> extends Observable<T> {
/**
* Just like RxJS itself, we can't actually make this method always type-safe
* because we would need non-final position spread params e.g.
* `static of<T>(...items: T, scheduler?: Scheduler): ActionsObservable<T>`
* which isn't possible in either JavaScript or TypeScript. So instead, we
* provide safe typing for up to 6 items, following by a scheduler.
*/
static of<T>(item1: T, scheduler?: Scheduler): ActionsObservable<T>;
static of<T>(item1: T, item2: T, scheduler?: Scheduler): ActionsObservable<T>;
static of<T>(item1: T, item2: T, item3: T, scheduler?: Scheduler): ActionsObservable<T>;
static of<T>(item1: T, item2: T, item3: T, item4: T, scheduler?: Scheduler): ActionsObservable<T>;
static of<T>(item1: T, item2: T, item3: T, item4: T, item5: T, scheduler?: Scheduler): ActionsObservable<T>;
static of<T>(item1: T, item2: T, item3: T, item4: T, item5: T, item6: T, scheduler?: Scheduler): ActionsObservable<T>;
static of<T>(...array: Array<T | Scheduler>): ActionsObservable<T>;

static from<T>(ish: ObservableInput<T>, scheduler?: Scheduler): ActionsObservable<T>;
static from<T, R>(ish: ArrayLike<T>, scheduler?: Scheduler): ActionsObservable<R>;

constructor(input$: Observable<T>);
lift(operator: Operator<any, T>) : ActionsObservable<T>;
ofType(...key: any[]) : ActionsObservable<T>;
Expand Down
10 changes: 6 additions & 4 deletions test/typings.ts
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import { createStore, applyMiddleware } from 'redux';
import { Observable } from 'rxjs/Observable';
import { asap } from 'rxjs/scheduler/asap';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/mapTo';
import 'rxjs/add/operator/map';
Expand Down Expand Up @@ -59,10 +60,6 @@ const rootEpic2 = combineEpics(epic1, epic2, epic3, epic4, epic5, epic6);
const epicMiddleware1: EpicMiddleware<FluxStandardAction> = createEpicMiddleware<FluxStandardAction>(rootEpic1);
const epicMiddleware2 = createEpicMiddleware(rootEpic2);

// should be a constructor that returns an observable
const input$ = Observable.create(() => {});
const action$: ActionsObservable<FluxStandardAction> = new ActionsObservable<FluxStandardAction>(input$);

const reducer = (state = [], action) => state.concat(action);
const store = createStore(
reducer,
Expand Down Expand Up @@ -115,4 +112,9 @@ expect(store.getState()).to.deep.equal([
{ "type": "sixth", "payload": "sixth-payload" }
]);

const input$ = Observable.create(() => {});
const action$1: ActionsObservable<FluxStandardAction> = new ActionsObservable<FluxStandardAction>(input$);
const action$2: ActionsObservable<FluxStandardAction> = ActionsObservable.of<FluxStandardAction>({ type: 'SECOND' }, { type: 'FIRST' }, asap);
const action$3: ActionsObservable<FluxStandardAction> = ActionsObservable.from<FluxStandardAction>([{ type: 'SECOND' }, { type: 'FIRST' }], asap);

console.log('typings.ts: OK');

0 comments on commit 0cba557

Please sign in to comment.