Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add ActionCreator#match method for single-argument type guard#42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
aikoven merged 6 commits intoaikoven:masterfromAlexanderOtavka:create-type-checker
Aug 24, 2017
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Change createTypeChecker -> ActionCreator#match
  • Loading branch information
@AlexanderOtavka
AlexanderOtavka committedAug 19, 2017
commit92d5b9766df69a993ba4ef5b15d926c20b3a0732
31 changes: 19 additions & 12 deletionsREADME.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -143,14 +143,11 @@ export const reducer = (state: State, action: Action): State => {

// epic.ts
import {Action} from 'redux';
import {createTypeChecker} from 'typescript-fsa';
import {Observable} from 'rxjs';
import {somethingAsync} from './actions';

const isSomethingAsyncStarted = createTypeChecker(somethingAsync.started);

export const epic = (actions$: Observable<Action>) =>
actions$.filter(isSomethingAsyncStarted)
actions$.filter(somethingAsync.started.match)
.delay(2000)
.map(action => {
// action.payload is inferred as {foo: string};
Expand DownExpand Up@@ -203,19 +200,29 @@ if (isType(action, somethingHappened)) {
}
```

### `createTypeChecker(actionCreator:ActionCreator):(action: Action) => boolean`
### `ActionCreator#match(action: Action): boolean`

Identical to `isType` except itenables partial application by returning
a single argumenttype guard function, suitablefor passing to a filtering
function like `Array.prototype.filter` or [RxJS](http://reactivex.io/rxjs/)'s
`Observable.prototype.filter`.
Identical to `isType` except itis exposed as a bound method of an action
creator. Since it is bound and takesa single argumentit is idealfor passing
to a filteringfunction like `Array.prototype.filter` or
[RxJS](http://reactivex.io/rxjs/)'s`Observable.prototype.filter`.

```ts
const somethingHappened = actionCreator<{foo: string}>('SOMETHING_HAPPENED');
const isSomethingHappened = createTypeChecker(somethingHappened)
const somethingElseHappened =
actionCreator<{bar: number}>('SOMETHING_ELSE_HAPPENED');

if (somethingHappened.match(action)) {
// action.payload has type {foo: string};
}

const actionArray = [
somethingHappened({foo: 'foo'}),
somethingElseHappened({bar: 5}),
]

const somethingHappenedArray:Action<{foo: string}> =
[somethingHappened({foo: 'foo'}), {}].filter(isSomethingHappened)
// somethingHappenedArray has inferred typeAction<{foo: string}>[];
const somethingHappenedArray = actionArray.filter(somethingHappened.match)
```

For more on using `Array.prototype.filter` as a type guard, see
Expand Down
2 changes: 1 addition & 1 deletionlib/index.d.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,9 +19,9 @@ export interface Failure<P, E> {
error: E;
}
export declare function isType<P>(action: AnyAction, actionCreator: ActionCreator<P>): action is Action<P>;
export declare function createTypeChecker<P>(actionCreator: ActionCreator<P>): (action: AnyAction) => action is Action<P>;
export interface ActionCreator<P> {
type: string;
match: (action: AnyAction) => action is Action<P>;
(payload: P, meta?: Meta): Action<P>;
}
export interface EmptyActionCreator extends ActionCreator<undefined> {
Expand Down
14 changes: 7 additions & 7 deletionslib/index.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,12 +4,6 @@ function isType(action, actionCreator) {
return action.type === actionCreator.type;
}
exports.isType = isType;
function createTypeChecker(actionCreator) {
return function (action) {
return isType(action, actionCreator);
};
}
exports.createTypeChecker = createTypeChecker;
function actionCreatorFactory(prefix, defaultIsError) {
if (defaultIsError === void 0) { defaultIsError = function (p) { return p instanceof Error; }; }
var actionTypes = {};
Expand All@@ -34,7 +28,13 @@ function actionCreatorFactory(prefix, defaultIsError) {
action.error = true;
}
return action;
}, { type: fullType, toString: function () { return fullType; } });
}, {
type: fullType,
toString: function () { return fullType; },
match: function (action) {
return action.type === fullType;
},
});
}
function asyncActionCreators(type, commonMeta) {
return {
Expand Down
15 changes: 8 additions & 7 deletionssrc/index.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,13 +28,9 @@ export function isType<P>(
return action.type === actionCreator.type;
}

export function createTypeChecker<P>(actionCreator: ActionCreator<P>) {
return (action: AnyAction): action is Action<P> =>
isType(action, actionCreator);
}

export interface ActionCreator<P> {
type: string;
match: (action: AnyAction) => action is Action<P>;
(payload: P, meta?: Meta): Action<P>;
}

Expand DownExpand Up@@ -80,7 +76,7 @@ export function actionCreatorFactory(

const base = prefix ? `${prefix}/` : "";

function actionCreator<P>(
function actionCreator<P>(
type: string, commonMeta?: Meta,
isError: ((payload: P) => boolean) | boolean = defaultIsError,
): ActionCreator<P> {
Expand DownExpand Up@@ -110,7 +106,12 @@ export function actionCreatorFactory(

return action;
},
{type: fullType, toString: () => fullType},
{
type: fullType,
toString: () => fullType,
match: (action: AnyAction): action is Action<P> =>
action.type === fullType,
},
);
}

Expand Down
10 changes: 4 additions & 6 deletionstests/typings/index.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
import actionCreatorFactory, {isType,createTypeChecker,AnyAction} from "typescript-fsa";
import actionCreatorFactory, {isType, AnyAction} from "typescript-fsa";


declare const action: AnyAction;
Expand DownExpand Up@@ -82,20 +82,18 @@ function testIsType() {
}
}

functiontestCreateTypeChecker() {
functiontestMatch() {
const withPayload = actionCreator<{foo: string}>('WITH_PAYLOAD');
const withoutPayload = actionCreator('WITHOUT_PAYLOAD');

const isWithPayload = createTypeChecker(withPayload)
if (isWithPayload(action)) {
if (withPayload.match(action)) {
const foo: string = action.payload.foo;

// typings:expect-error
action.payload.bar;
}

const isWithoutPayload = createTypeChecker(withoutPayload)
if (isWithoutPayload(action)) {
if (withoutPayload.match(action)) {
// typings:expect-error
const foo: {} = action.payload;
}
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp