This repository was archived by the owner on Feb 14, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 87
This repository was archived by the owner on Feb 14, 2023. It is now read-only.
Global reducers do not work in TypeScript 4. #174
Copy link
Copy link
Open
Labels
bugSomething isn't working as expected.Something isn't working as expected.help wantedThe owner cannot resolve this on their own.The owner cannot resolve this on their own.
Description
Type inference for reducers appears to be broken with TypeScript v4 and reactn 2.2.7.
For some reason, useDispatch(reducerName) results in the following error:
// ERROR: Type of property 'increment' circularly references itself in mapped type 'DispatcherMap<State, Reducers>'.ts(2615)
Below is an example.
// global.d.ts
import 'reactn';
import { Dispatch } from 'react';
declare module 'reactn/default' {
export interface Reducers {
increment: (global: State, dispatch: Dispatch, i: number) => Pick<State, 'x'>
}
export interface State {
x: number
}
}
// App.tsx
import React, { setGlobal, useGlobal, useDispatch, addReducer } from 'reactn'
interface ProviderState {
x: number
}
const INITIAL_STATE: ProviderState = {
x: 0
}
setGlobal(INITIAL_STATE)
addReducer('increment', (global, dispatch, i = 0) => {
return { x: global.x + i }
})
export default function () {
const increment = useDispatch('increment') // <--- ERROR
const [ x, ] = useGlobal('x')
return (
<div>
<p>{}</p>
<button onClick={() => increment(1) }>Increment</button>
</div>
)
}
Here is a second, related issue. The types for Provider.useDispatch does not accept strings even though the global version does.
import React, { createProvider } from 'reactn'
interface ProviderState {
x: number
}
const INITIAL_STATE: ProviderState = {
x: 0
}
const Provider = createProvider(INITIAL_STATE)
Provider.addReducer('increment', (state, dispatch, x = 0) => ({
x: x + 1
})
export default function () {
const increment = Provider.useDispatch('increment') // <---- ERROR
const [ x, ] = Provider.useGlobal('x')
return (
<div>
<p>{}</p>
<button onClick={() => increment() }>Increment</button>
</div>
)
}
ERROR
No overload matches this call.
Overload 1 of 4, '(reducer: Reducer<ProviderState, Reducers, any[], NewGlobalState<ProviderState>>): Dispatcher<ProviderState, any[]>', gave the following error.
Argument of type 'string' is not assignable to parameter of type 'Reducer<ProviderState, Reducers, any[], NewGlobalState<ProviderState>>'.
Overload 2 of 4, '(reducer: never): Dispatcher<ProviderState, never>', gave the following error.
Argument of type 'string' is not assignable to parameter of type 'never'.
Metadata
Metadata
Assignees
Labels
bugSomething isn't working as expected.Something isn't working as expected.help wantedThe owner cannot resolve this on their own.The owner cannot resolve this on their own.