-
Notifications
You must be signed in to change notification settings - Fork 23
Description
Using this code sample:
export const App = () => {
const [] = useMethods(
state => ({
set(value: number) {
return {
...state,
count: value
};
}
}),
initialState
);
};
interface State {
count: number;
}
const initialState: State = {
count: 0
};In (for example) vscode, if you rename the count property within the State interface, it will not propagate the count property in the object being returned from the increment method. Similar operations such as "go to definition" and "find references" will also not work.
Although the typings do require that the increment method returns something that matches the State interface structurally, it has lost the reference to the actual interface.
Aside from being a bit inconvenient, this is a potential hazard because TS allows extra properties in the returned object. Therefore, if one was to rename the count property on the interface the reducer would continue to compile without errors, and would fail in an unspecified manner at runtime instead.
Workarounds for this include:
- only use immer to manipulate the state (i.e. always return
void) - type the reducer method return types explicitly:
set(value: number): State