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

Fix incorrect parameter type for StoreEnhancer #3769

Closed

Conversation

Methuselah96
Copy link
Member

@Methuselah96 Methuselah96 commented May 9, 2020


name: "Fix incorrect parameter type for StoreEnhancer"
about: Fix incorrect parameter type for StoreEnhancer

PR Type

Does this PR add a new feature, or fix a bug?

Fix a bug.

Why should this PR be included?

It resolves the issue found in #3768. Since we don't know what the generic types for StoreEnhancerStoreCreator should be, they should be typed as unknown.

Checklist

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Is there an existing issue for this PR?
  • Have the files been linted and formatted?
  • Have the docs been updated to match the changes in the PR?
  • Have the tests been updated to match the changes in the PR?
  • Have you run the tests locally to confirm they pass?

Bug Fixes

What is the current behavior, and the steps to reproduce the issue?

See issue.

What is the expected behavior?

For TypeScript to throw an error when trying to access a property that doesn't exist.

Code

type MyEnhancerType = { enhancedStuff: number };

function createEnhancer(): StoreEnhancer<MyEnhancerType> {
  return (createStore) => <S, A extends Action>(
    reducer: Reducer<S, A>,
    initialState?: PreloadedState<S>
  ): Store<S, A, never, MyEnhancerType> & MyEnhancerType => {
    // This store that I just created does not have the enhancor applied to it yet
    // It also doesn't know what enhancors have been applied to it
    const nextStore = createStore(reducer, initialState);

    // Now this correctly produces an error
    const propertyThatsNotOnStoreYet = nextStore.enhancedStuff;

    // It's not until here that we have an enhanced store
    // And should be able to access that property
    const enhancedStore = enhanceStore(nextStore);
    // And this still works
    const correctPropertyAccess = enhancedStore.enhancedStuff;

    return enhancedStore;
  };
}

function enhanceStore<S, A extends Action>(store: Store<S, A, unknown, unknown>): Store<S, A, never, MyEnhancerType> & MyEnhancerType {
  return {
    ...store,
    enhancedStuff: 5,
  }
}
Output
function createEnhancer() {

    return (createStore) => (reducer, initialState) => {

        // This store that I just created does not have the enhancor applied to it yet

        // It also doesn't know what enhancors have been applied to it

        const nextStore = createStore(reducer, initialState);

        // Now this correctly produces an error

        const propertyThatsNotOnStoreYet = nextStore.enhancedStuff;

        // It's not until here that we have an enhanced store

        // And should be able to access that property

        const enhancedStore = enhanceStore(nextStore);

        // And this still works

        const correctPropertyAccess = enhancedStore.enhancedStuff;

        return enhancedStore;

    };

}

function enhanceStore(store) {

    return Object.assign(Object.assign({}, store), { enhancedStuff: 5, 

        // Ignore this for now. See https://github.com/reduxjs/redux/issues/3767 for more discussion

        replaceReducer(nextReducer) {

            return store.replaceReducer(nextReducer);

        } });

}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

How does this PR fix the problem?

My fixing the type of StoreEnhancer. Resolves #3768.

@netlify
Copy link

netlify bot commented May 9, 2020

Deploy preview for redux-docs ready!

Built with commit d047a9b

https://deploy-preview-3769--redux-docs.netlify.app

@Methuselah96
Copy link
Member Author

Methuselah96 commented May 9, 2020

Note there are type tests in this PR that would fail if #3770 got merged. It's not recommended to merge this PR yet.

@Methuselah96
Copy link
Member Author

Closed for now while I think about this some more.

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

Successfully merging this pull request may close these issues.

StoreEnhancer has incorrect parameter type
1 participant