monolite
monolite copied to clipboard
Type inference fails with optional state parameters
We're using monolite to customize parts of our mock state trees for particular test suites, and I'm trying to upgrade from 0.4.6 to 0.8.0. I notice the syntax for set
has changed to take the new state as a third argument. No problem, fixed that up. I'm having trouble with the second argument (the accessor function). When the portion of state being modified is an optional parameter of its interface, I get this error:
Argument of type '(_: StateTree) => string | undefined' is not assignable to parameter of type 'Accessor<StateTree, {}>'.
Type '(_: StateTree) => string | undefined' is not assignable to type 'AccessorFunction<StateTree, {}>'.
Type 'string | undefined' is not assignable to type '{}'.
Type 'undefined' is not assignable to type '{}'. [2345]
It seems that the AccessorFunction
's second generic type is not being properly inferred when it is optional (string | undefined
in this case).
Here's some sample code that reproduces the above error:
interface StateTree {
readonly myBranch: StateTreeBranch;
}
interface StateTreeBranch {
readonly optionalState?: string;
}
const mockStateTree: StateTree = {
myBranch: {
optionalState: 'original_state',
}
};
const updatedStateTree = monolite.set(
mockStateTree,
_ => _.myBranch.optionalState,
'some_new_state',
);
This is literally the first issue I've ever opened on GitHub, so let me know what I'm doing wrong, what else you need, etc.