esamattis / immer-reducer

Type-safe and terse reducers with Typescript for React Hooks and Redux
http://npm.im/immer-reducer
MIT License
225 stars 15 forks source link

State with genericity #52

Open ThomasMerant opened 4 years ago

ThomasMerant commented 4 years ago

Hello, I would like to create standard Reducer like this :

import { ImmerReducer } from "immer-reducer";

interface HttpListResult<T> {
    query: HttpListQuery;
    total?: number;
    data?: T[];
}

interface ListViewState<T> extends HttpListResult<T> {
    isLoading?: boolean;
}

export abstract class MyReducerBase<T> extends ImmerReducer<ListViewState<T>> {
    getAll(query: HttpListQuery) {
        this.draftState = {
            query,
            isLoading: true,
        };
    }

    getAllResult(result: HttpListResult<T>) {
        this.draftState = {
            ...result,
            isLoading: false,
        };
    }
}

But in the draftState, data property is typed in Draft\<T>...

What is my error ?

yrral86 commented 3 years ago

With typescript 3.8 I see no type errors with your code, but I don't know what your type definition is for HttpListQuery.