From 61248ceda1fdb11f0bc882f6fa4df7e4ce5a5155 Mon Sep 17 00:00:00 2001 From: azu Date: Thu, 6 Apr 2017 19:35:55 +0900 Subject: [PATCH] fix(typescript): fix reduce return type --- package.json | 2 ++ src/ReduceState.ts | 2 +- src/ReduceStore.ts | 11 ++++------- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 3c4a3f1..3301a9b 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" } } diff --git a/src/ReduceState.ts b/src/ReduceState.ts index 2df45ae..e3d94c6 100644 --- a/src/ReduceState.ts +++ b/src/ReduceState.ts @@ -28,7 +28,7 @@ export class ReduceState { * @param {Object} payload * @returns {ReduceState} */ - reduce(payload: Payload): ReduceState { + reduce(payload: Payload): T | this { switch (payload.type) { default: return this; diff --git a/src/ReduceStore.ts b/src/ReduceStore.ts index 3f97dde..d196474 100644 --- a/src/ReduceStore.ts +++ b/src/ReduceStore.ts @@ -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 extends Store { + state: T | null; constructor() { super(); - /** - * @type {ReduceState} - **/ this.state = null; // Automatically handling onDispatch this.onDispatch(this._onDispatch.bind(this)); @@ -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. @@ -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(payload)); } }