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

Typescript error: Type 'unknown' is not assignable to type 'never' #4076

Closed
Guymestef opened this issue Apr 29, 2021 · 11 comments
Closed

Typescript error: Type 'unknown' is not assignable to type 'never' #4076

Guymestef opened this issue Apr 29, 2021 · 11 comments

Comments

@Guymestef
Copy link

What is the current behavior?

Redux set never as type instead of unknown:

The types of 'router.location.state' are incompatible between these types.
Type 'unknown' is not assignable to type 'never'.

Steps to Reproduce

import { connectRouter, LocationChangeAction, RouterState } from 'connected-react-router';
import { History } from 'history';
import { applyMiddleware, combineReducers, compose, createStore, Store, CombinedState, AnyAction } from 'redux';

export type ApplicationState = { router: RouterState };

const createRootReducer = (history: History) => combineReducers({
    router: connectRouter(history),
});

export default function configureStore(history: History, initialState?: ApplicationState): Store<CombinedState<any>, AnyAction | LocationChangeAction<any>> {
    const store = createStore(
        createRootReducer(history), // root reducer with router state
        initialState,
    );

    return store;
}

What is the expected behavior?

Use unknown instead of never

Environment Details

Redux 4.1.0
Chrome
Windows 10
Did this work in previous versions of Redux? Yes

@markerikson
Copy link
Contributor

This really isn't a meaningful issue report that we can act on. I don't know what the rest of this error message is, and I don't know how this error message actually relates to Redux at all. If anything, this is likely related to use of connected-react-router or the hardcoded return type for your configureStore function.

Please follow our recommended patterns for using Redux with TypeScript, including use of Redux Toolkit's configureStore method and inferring the type of RootState.

If you're still having issues after that, please provide a reproduction of the problem as either a CodeSandbox or a Github repo that demonstrates what's happening.

@Guymestef
Copy link
Author

The error is not on the configureStore but on createStore.
My ApplicationState contains the property router: RouterState, and createStore is mis-infering the type for router.
createStore is telling: router.location.state is of type never
RouterState is telling: router.location.state is of type unknown

@markerikson
Copy link
Contributor

As I said, this is likely due to your hardcoded TS return type or the use of connected-react-router, and there's nothing I can do without an actual reproduction of the issue.

@Guymestef
Copy link
Author

I removed the hardcoded return type and I still have the issue.
Sorry to insist since I'm not sure if the issue is from redux typings or connected-react-router, but by looking at the redux typings, I see that the never I get is coming from:

export type PreloadedState<S> = Required<S> extends {
  [$CombinedState]: undefined
}
  ? S extends CombinedState<infer S1>
    ? {
        [K in keyof S1]?: S1[K] extends object ? PreloadedState<S1[K]> : S1[K]
      }
    : never
  : {
      [K in keyof S]: S[K] extends string | number | boolean | symbol
        ? S[K]
        : PreloadedState<S[K]>
    }

by modifying this typing like that:

export type PreloadedState<S> = Required<S> extends EmptyObject
 ? S extends CombinedState<infer S1>
   ? {
       [K in keyof S1]?: S1[K] extends object ? PreloadedState<S1[K]> : S1[K]
     }
   : unknown
 : {
     [K in keyof S]: S[K] extends string | number | boolean | symbol
       ? S[K]
       : PreloadedState<S[K]>
   }

typescript is not complaining any longer.

@phryneas
Copy link
Member

Please provide a code snippet that actually reproduces the issue - the code you have there runs just 100% fine in TypeScript, as you can see in this TS playground

@Guymestef
Copy link
Author

Hello,

I finally pinpointed the source of the issue.
On my project, I set strictNullChecks: true in my tsconfig file, and this changes the output of the following tests.

With strictNullChecks: true, test is of type string, and with strictNullChecks: false, test is of type number

type test = unknown extends {} ? number : string;

So the output of PreloadedState changes with the value of strictNullChecks too.

For example:
type shouldBeUnknown = PreloadedState<{state: unknown}>;
With strictNullChecks: true, shouldBeUnknown is of type { state: never; }
With strictNullChecks: false, shouldBeUnknown is of type { state: {}; }

With a playground showing the typing error: TS playground

@phryneas
Copy link
Member

phryneas commented May 3, 2021

So the output of PreloadedState changes with the value of strictNullChecks too.

That part is pretty natural. If you set strictNullChecks: false, the programming language changes behavior massively. You cannot expect any library to cater for the fact that you just disabled a part of the programming language.
90% of the libraries would be TS code to feature-detect the user's configuration on which features are enabled just now or not, since TS itself doesn't even provide any tools to check for that.

@Guymestef
Copy link
Author

I didn't disable it, I enable it.

@phryneas phryneas mentioned this issue May 3, 2021
6 tasks
@phryneas
Copy link
Member

phryneas commented May 3, 2021

Please see if #4078 resolves your issue

@Guymestef
Copy link
Author

It resolves it 👍

@jucrouzet
Copy link

I encountered the same issue and #4078 fixes it.

Thanks @phryneas waiting for the PR to be merged.

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

4 participants