Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Redux 4.2.0 breaks PreloadedState type? #4423

Closed
sekoyo opened this issue Oct 10, 2022 · 2 comments
Closed

Redux 4.2.0 breaks PreloadedState type? #4423

sekoyo opened this issue Oct 10, 2022 · 2 comments

Comments

@sekoyo
Copy link

sekoyo commented Oct 10, 2022

PreloadedState was previously working in that top levels keys were optional:

/**
 * Recursively makes combined state objects partial. Only combined state _root
 * objects_ (i.e. the generated higher level object with keys mapping to
 * individual reducers) are partial.
 */

After upgrading 4.1.2 -> 4.2.0 however, the store keys are no longer optional:

const rootReducer = combineReducers({
  assetConfig: AssetConfigReducer,
  coinConfiguration: CoinConfigurationReducer,
  buyCrypto: BuyCryptoReducer,
  chat: ChatReducer,
  coinDetails: CoinDetailsReducer,
  convertCrypto: ConvertCryptoReducer,
  depositAddress: DepositReducer,
  earn: EarnReducer,
  indices: IndicesReducer,
  markets: MarketsReducer,
  savedAddress: SavedAddressReducer,
  security: SecurityReducer,
  stake: StakeReducer,
  trade: TradeReducer,
  user: UserReducer,
  walletSummary: WalletSummaryReducer,
  walletTxnHistory: TransactionHistoryReducer,
  withdraw: WithdrawReducer,
  KYC: KYCReducer,
});

type RootState = ReturnType<typeof rootReducer>;

PreloadedState<RootState> now requires that all reducer keys are present.

Previously you could pass in just the parts of the store you want to be non-default:

const setupStore = (preloadedState?: PreloadedState<RootState>) =>
  configureStore({
    reducer: RootReducer,
    preloadedState,
    // ....
  });

const renderWithProviders = (
  children: React.ReactNode,
  preloadedState?: PreloadedState<RootState>,
) => {
  const store = setupStore({
    // ...
    ...preloadedState,
  });
  return renderer.create(<Provider store={store}>...</Provider>);
}

const tree = renderWithProviders(
      <CoinDetailsScreen navigation={navigation} route={route} />,
      {
        earn: {
          ...initialEarnState,
          displayContracts: [],
        },
      },
    );

Now such code results in a TS error as all reducer keys are expected:

|Argument of type '{ earn: { displayContracts: never[]; isLoading: boolean; error: ErrorState; status: boolean; subscriptions: Subscription[]; subscriptionBalances: SubscriptionBalance[]; activeContracts: Contract[]; balanceDetails: { ...; }; subscribeTransaction: SubscribeTransaction; }; }' is not assignable to parameter of type '{ readonly [$CombinedState]?: undefined; assetConfig: { assets: { [x: string]: { asset: string; currency: string; name: string; majorCurrency: string; currencyType: string; scale: number; enabled: boolean; ... 4 more ...; networks: { ...; }[]; } | undefined; }; assetToCurrency: { ...; }; networks: { ...; }; }; ... 1...'.
  Type '{ earn: { displayContracts: never[]; isLoading: boolean; error: ErrorState; status: boolean; subscriptions: Subscription[]; subscriptionBalances: SubscriptionBalance[]; activeContracts: Contract[]; balanceDetails: { ...; }; subscribeTransaction: SubscribeTransaction; }; }' is missing the following properties from type '{ readonly [$CombinedState]?: undefined; assetConfig: { assets: { [x: string]: { asset: string; currency: string; name: string; majorCurrency: string; currencyType: string; scale: number; enabled: boolean; ... 4 more ...; networks: { ...; }[]; } | undefined; }; assetToCurrency: { ...; }; networks: { ...; }; }; ... 1...': assetConfig, coinConfiguration, buyCrypto, chat, and 14 more.ts(2345)
@markerikson
Copy link
Contributor

That shouldn't be the case - the only change in 4.2.0 was the deprecation of createStore, no changes to the typedefs:

v4.1.2...v4.2.0

Did you upgrade any other packages?

@sekoyo
Copy link
Author

sekoyo commented Oct 10, 2022

Thank you I did, will investigate

@sekoyo sekoyo closed this as completed Oct 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants