Skip to content

StoreEnhancer has incorrect parameter type #3768

Closed
@Methuselah96

Description

@Methuselah96

Prior Issues

Introduced in #3524.

What is the current behavior?

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 enhancer applied to it yet
    // It also doesn't know what enhancers have been applied to it
    const nextStore = createStore(reducer, initialState);

    // And yet we can access a property that we have yet to add.
    // This should produce a type error, but it doesn't.
    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);
    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);
        // And yet we can access a property that we have yet to add.
        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);
        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

Steps to Reproduce

See code above.

What is the expected behavior?

Trying to access enhanced properties right after calling createStore should result in a type error.

Environment Details

N/A

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions