Skip to content

Instantly share code, notes, and snippets.

@cjol
Last active July 27, 2018 07:11
Show Gist options
  • Save cjol/cbfb7b9efcfd91a12a379094057bcaac to your computer and use it in GitHub Desktop.
Save cjol/cbfb7b9efcfd91a12a379094057bcaac to your computer and use it in GitHub Desktop.
isActionOf demonstration
import { ActionType } from "typesafe-actions";
import { ActionsObservable } from "redux-observable";
export declare const actions: {
toggle: () => {
type: "todos/TOGGLE";
};
add: () => {
type: "todos/ADD";
};
dummy: () => {
type: "todos/DUMMY";
};
};
export declare type TestActions = ActionType<typeof actions>;
export declare const epic1: (action$: ActionsObservable<{
type: "todos/TOGGLE";
} | {
type: "todos/ADD";
} | {
type: "todos/DUMMY";
}>) => import("rxjs/internal/Observable").Observable<{
type: "todos/TOGGLE";
} | {
type: "todos/ADD";
} | {
type: "todos/DUMMY";
}>;
export declare const myFilter: (action: {
type: string;
}) => action is {
type: string;
} | {
type: "todos/TOGGLE";
} | {
type: "todos/ADD";
};
export declare const epic2: (action$: ActionsObservable<{
type: "todos/TOGGLE";
} | {
type: "todos/ADD";
} | {
type: "todos/DUMMY";
}>) => import("rxjs/internal/Observable").Observable<{
type: string;
} | {
type: "todos/TOGGLE";
} | {
type: "todos/ADD";
}>;
import { createAction, ActionType, isActionOf } from "typesafe-actions";
import { filter, tap } from "rxjs/operators";
import { ActionsObservable } from "redux-observable";
export const actions = {
toggle: createAction("todos/TOGGLE"),
add: createAction("todos/ADD"),
dummy: createAction("todos/DUMMY")
};
export type TestActions = ActionType<typeof actions>;
export const epic1 = (action$: ActionsObservable<TestActions>) =>
action$.pipe(
filter(isActionOf([actions.add, actions.toggle])),
tap(x => x.type) // x.type: "todos/TOGGLE" | "todos/ADD" | "todos/DUMMY"
);
export const myFilter = isActionOf([actions.add, actions.toggle]);
export const epic2 = (action$: ActionsObservable<TestActions>) =>
action$.pipe(
filter(myFilter),
tap(x => x.type) // x.type: string | "todos/ADD" | "todos/TOGGLE"
);
{
"name": "typescript-test",
"version": "1.0.0",
"description": "",
"main": "test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "tsc -p tsconfig.json --declaration",
"version": "tsc --version"
},
"author": "Christopher Little <christopher@littlehq.uk> (littlesavage.net)",
"license": "ISC",
"dependencies": {
"redux": "^4.0.0",
"redux-observable": "^1.0.0",
"rxjs": "^6.2.2",
"typesafe-actions": "^2.0.4",
"typescript": "^2.9.2"
}
}
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"importHelpers": true,
"jsx": "react-native",
"lib": ["es2015", "es2016", "es2017"],
"types": [],
"module": "commonjs",
"moduleResolution": "node",
"noEmitHelpers": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"sourceMap": false,
"strict": true,
"target": "es6"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment