question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect TypeScript declaration

See original GitHub issue

TypeScript type declaration created in #27 is incorrect. It doesn’t allow combining of multiple reducers without an initial state.

Both examples below are taken from tests (with some modifications).

const initialState = {A: 0, B: 0};
type State = typeof initialState;

// this one is OK
const reducer1 = reduceReducer(initialState, (state: State, action: Action) => ({
  ...state,
  A: state.A + 1
}));

// this one fails because first argument is expected to be initial state
const reducer2 = reduceReducer(
  (state: State, action: Action) => ({...state, A: state.A + 2}),
  (state: State, action: Action) => ({...state, B: state.B * 2}),
);

The second example fails with the following error:

Argument of type '(state: { A: number; B: number; }, action: Action) => { B: number; A: number; }' is not assignable to parameter of type 'Reducer<(state: { A: number; B: number; }, action: Action) => { A: number; B: number; }>'.
Types of parameters 'state' and 'state' are incompatible.
Type '(state: { A: number; B: number; }, action: Action) => { A: number; B: number; }' is missing the following properties from type '{ A: number; B: number; }': A, B
Link to TypeScript Playground

https://www.typescriptlang.org/play/index.html#src=%2F%2F type declarations%2C copy from https%3A%2F%2Fgithub.com%2Fredux-utilities%2Freduce-reducers%2Fblob%2Fmaster%2Findex.d.ts type Action %3D { type%3A string%3B }%3B type Reducer<S> %3D (state%3A S%2C action%3A Action) %3D> S%3B declare function reduceReducer<S>( initialState%3A S%2C ...reducers%3A Array<Reducer<S>> )%3A Reducer<S>%3B %2F%2F reduce-reducers use%2C modified examples from https%3A%2F%2Fgithub.com%2Fredux-utilities%2Freduce-reducers%2Fblob%2Fmaster%2Ftest%2Findex.test.js const initialState %3D {A%3A 0%2C B%3A 0}%3B type State %3D typeof initialState%3B %2F%2F this one is OK const reducer1 %3D reduceReducer(initialState%2C (state%3A State%2C action%3A Action) %3D> ({ ...state%2C A%3A state.A %2B 1 }))%3B%0D%0A%0D%0A%2F%2F%20this%20one%20fails%20because%20first%20argument%20is%20expected%20to%20be%20initial%20state%0D%0Aconst%20reducer2%20%3D%20reduceReducer(%0D%0A%20%20(state%3A%20State%2C%20action%3A%20Action)%20%3D%3E%20(%7B…state%2C%20A%3A%20state.A%20%2B%202%7D)%2C%0D%0A%20%20(state%3A%20State%2C%20action%3A%20Action)%20%3D%3E%20(%7B…state%2C%20B%3A%20state.B%20*%202%7D)%2C%0D%0A)%3B%0D%0A%0D%0A%2F%2F%20above%20error%3A%0D%0A%2F%2F%20Argument%20of%20type%20’(state%3A%20%7B%20A%3A%20number%3B%20B%3A%20number%3B%20%7D%2C%20action%3A%20Action)%20%3D%3E%20%7B%20B%3A%20number%3B%20A%3A%20number%3B%20%7D’%20is%20not%20assignable%20to%20parameter%20of%20type%20’Reducer%3C(state%3A%20%7B%20A%3A%20number%3B%20B%3A%20number%3B%20%7D%2C%20action%3A%20Action)%20%3D%3E%20%7B%20A%3A%20number%3B%20B%3A%20number%3B%20%7D%3E’.%0D%0A%2F%2F%20Types%20of%20parameters%20’state’%20and%20’state’%20are%20incompatible.%0D%0A%2F%2F%20Type%20’(state%3A%20%7B%20A%3A%20number%3B%20B%3A%20number%3B%20%7D%2C%20action%3A%20Action)%20%3D%3E%20%7B%20A%3A%20number%3B%20B%3A%20number%3B%20%7D’%20is%20missing%20the%20following%20properties%20from%20type%20’%7B%20A%3A%20number%3B%20B%3A%20number%3B%20%7D’%3A%20A%2C%20B%0D%0A

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
timchecommented, May 2, 2019

Fixed in v1.0.2.

Thanks everyone 👏

0reactions
tuulingcommented, May 2, 2019

@vhenzl Right. About point nr1 Did not notice that you could call it without initialState all together. About points 2 and 3 I can’t comment as I have not used or needed to use those.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript declaration files gets built in incorrect directory #854
This project fails to build because it can't find the declaration file for @my-workspace/project-b . Issue. In my actual project I have 12...
Read more >
How to overwrite incorrect TypeScript type definition installed ...
I would copy the declaration files from DefinitelyTyped, modify them locally, send a PR to DefinitelyTyped, and then follow the advice given on...
Read more >
TSConfig Reference - Docs on every TSConfig option
# Declaration - declaration. Generate .d.ts files for every TypeScript or JavaScript file inside your project. These .d.ts ...
Read more >
TypeScript Done Wrong - OpenReplay Blog
That happens because we think type declaration has to be done when you declare the variable and at that time, you may not...
Read more >
TypeScript errors and how to fix them
error TS2664: Invalid module name in augmentation, module ' gas-local ' cannot be found. Broken Code ❌. main.ts. 1, declare ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found