Skip to content

Commit

Permalink
fix(typescript): fix reduce return type
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Apr 6, 2017
1 parent 1c832b4 commit 61248ce
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"types": "lib/index.d.ts",
"scripts": {
"test": "mocha test/",
"prebuild": "rimraf lib",
"build": "cross-env NODE_ENV=production tsc -p .",
"watch": "tsc -p . --watch",
"prepublish": "npm run --if-present build"
Expand All @@ -42,6 +43,7 @@
"espower-typescript": "^8.0.0",
"mocha": "^3.2.0",
"power-assert": "^1.4.2",
"rimraf": "^2.6.1",
"typescript": "^2.2.2"
}
}
2 changes: 1 addition & 1 deletion src/ReduceState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class ReduceState {
* @param {Object} payload
* @returns {ReduceState}
*/
reduce(payload: Payload): ReduceState {
reduce<T extends ReduceState>(payload: Payload): T | this {
switch (payload.type) {
default:
return this;
Expand Down
11 changes: 4 additions & 7 deletions src/ReduceStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
const assert = require("assert");
import { Store, Payload } from "almin";
import { ReduceState } from "./ReduceState";
export class ReduceStore extends Store {
state: ReduceState | null;
export class ReduceStore<T extends ReduceState> extends Store {
state: T | null;

constructor() {
super();
/**
* @type {ReduceState}
**/
this.state = null;
// Automatically handling onDispatch
this.onDispatch(this._onDispatch.bind(this));
Expand All @@ -21,7 +18,7 @@ export class ReduceStore extends Store {
* If `newState` is the same with `tis.state`, don't set.
* @param {ReduceState} newState
*/
setState(newState: ReduceState): void {
setState(newState: T): void {
if (process.env.NODE_ENV !== "production") {
assert(this.state !== null, "this.state is null, should be set to this.state in constructor.");
assert(newState instanceof this.state!.constructor, `newState should be instanceof exist this.state.constructor.
Expand All @@ -46,6 +43,6 @@ this.state.constructor: ${this.state!.constructor}
assert(this.state !== null, "this.state is null, should be set to this.state in constructor.");
assert(payload !== undefined, `payload is undefined: ${payload}`);
}
this.setState(this.state!.reduce(payload));
this.setState(this.state!.reduce<T>(payload));
}
}

0 comments on commit 61248ce

Please # to comment.