Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

change reducer type validation place #4452

Merged
merged 2 commits into from
Dec 15, 2022

Conversation

Ahmed-Hakeem
Copy link
Contributor

@Ahmed-Hakeem Ahmed-Hakeem commented Dec 14, 2022

change reducer type validation to be at the very start of createStore function since all operations after this condition is met are neglected, and this also forces users who pass their own enhancers to pass the reducer as a function

@Ahmed-Hakeem Ahmed-Hakeem changed the title change 'reducer is not a function' err change 'reducer is not a function' err place Dec 14, 2022
@codesandbox-ci
Copy link

codesandbox-ci bot commented Dec 14, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit c0bb7f7:

Sandbox Source
Vanilla Configuration
Vanilla Typescript Configuration

@Ahmed-Hakeem Ahmed-Hakeem changed the title change 'reducer is not a function' err place change reducer type validation place Dec 14, 2022
@timdorr
Copy link
Member

timdorr commented Dec 14, 2022

Thanks, but this functionally doesn't do anything. In addition, the type check would fail for the wrong kind of reducer.

@timdorr timdorr closed this Dec 14, 2022
@Ahmed-Hakeem
Copy link
Contributor Author

Ahmed-Hakeem commented Dec 14, 2022

Thanks, but this functionally doesn't do anything. In addition, the type check would fail for the wrong kind of reducer.

It does, check the below cases :

  1. function will return before throwing this error, check the comment below
if (
    (typeof preloadedState === 'function' && typeof enhancer === 'function') ||
    (typeof enhancer === 'function' && typeof arguments[3] === 'function')
  ) {
    throw new Error(
      'It looks like you are passing several store enhancers to ' +
        'createStore(). This is not supported. Instead, compose them ' +
        'together to a single function. See https://redux.js.org/tutorials/fundamentals/part-4-store#creating-a-store-with-enhancers for an example.'
    )
  }
  if (typeof preloadedState === 'function' && typeof enhancer === 'undefined') {
    enhancer = preloadedState as StoreEnhancer<Ext, StateExt>
    preloadedState = undefined
  }

  if (typeof enhancer !== 'undefined') {
    if (typeof enhancer !== 'function') {
      throw new Error(
        `Expected the enhancer to be a function. Instead, received: '${kindOf(
          enhancer
        )}'`
      )
    }
    /**function will return before throwing reducer's type error,
    but the returned createStore call or its sub calls from the enhancer will throw if you pass it wrong reducer type and this case doesn't happen again for sure
   */
    return enhancer(createStore)(
      reducer,
      preloadedState as PreloadedState<S>
    ) as Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext
  }

  if (typeof reducer !== 'function') {
    throw new Error(
      `Expected the root reducer to be a function. Instead, received: '${kindOf(
        reducer
      )}'`
    )
  }
//so if you pass these types function will return before throwing this error, assume enhancer is of type function
const store = createStore(undefined, {} ,enhancer); 

so I think throwing reducer's type error from the very start will be better since there will be no cost from putting it at the very start.

also, doing all these conditions before throwing the reducer's type error is not the best even if it's low cost, as passing the wrong reducer type will in the end throw an error and break all that as you have said.

@timdorr
Copy link
Member

timdorr commented Dec 15, 2022

Hmm, I guess that does make sense. I'm so used to TS now that I don't really think about these things anymore! 😄

@timdorr timdorr reopened this Dec 15, 2022
@timdorr timdorr merged commit fbfe514 into reduxjs:master Dec 15, 2022
@Ahmed-Hakeem
Copy link
Contributor Author

Hmm, I guess that does make sense. I'm so used to TS now that I don't really think about these things anymore! 😄

Never mind ..... thank you ❤️

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants