Skip to content

Commit 61248ce

Browse files
committed
fix(typescript): fix reduce return type
1 parent 1c832b4 commit 61248ce

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"types": "lib/index.d.ts",
1717
"scripts": {
1818
"test": "mocha test/",
19+
"prebuild": "rimraf lib",
1920
"build": "cross-env NODE_ENV=production tsc -p .",
2021
"watch": "tsc -p . --watch",
2122
"prepublish": "npm run --if-present build"
@@ -42,6 +43,7 @@
4243
"espower-typescript": "^8.0.0",
4344
"mocha": "^3.2.0",
4445
"power-assert": "^1.4.2",
46+
"rimraf": "^2.6.1",
4547
"typescript": "^2.2.2"
4648
}
4749
}

src/ReduceState.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class ReduceState {
2828
* @param {Object} payload
2929
* @returns {ReduceState}
3030
*/
31-
reduce(payload: Payload): ReduceState {
31+
reduce<T extends ReduceState>(payload: Payload): T | this {
3232
switch (payload.type) {
3333
default:
3434
return this;

src/ReduceStore.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
const assert = require("assert");
44
import { Store, Payload } from "almin";
55
import { ReduceState } from "./ReduceState";
6-
export class ReduceStore extends Store {
7-
state: ReduceState | null;
6+
export class ReduceStore<T extends ReduceState> extends Store {
7+
state: T | null;
88

99
constructor() {
1010
super();
11-
/**
12-
* @type {ReduceState}
13-
**/
1411
this.state = null;
1512
// Automatically handling onDispatch
1613
this.onDispatch(this._onDispatch.bind(this));
@@ -21,7 +18,7 @@ export class ReduceStore extends Store {
2118
* If `newState` is the same with `tis.state`, don't set.
2219
* @param {ReduceState} newState
2320
*/
24-
setState(newState: ReduceState): void {
21+
setState(newState: T): void {
2522
if (process.env.NODE_ENV !== "production") {
2623
assert(this.state !== null, "this.state is null, should be set to this.state in constructor.");
2724
assert(newState instanceof this.state!.constructor, `newState should be instanceof exist this.state.constructor.
@@ -46,6 +43,6 @@ this.state.constructor: ${this.state!.constructor}
4643
assert(this.state !== null, "this.state is null, should be set to this.state in constructor.");
4744
assert(payload !== undefined, `payload is undefined: ${payload}`);
4845
}
49-
this.setState(this.state!.reduce(payload));
46+
this.setState(this.state!.reduce<T>(payload));
5047
}
5148
}

0 commit comments

Comments
 (0)