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

Type inference fails with optional state parameters #45

Open
jonthanon opened this issue Dec 12, 2018 · 0 comments
Open

Type inference fails with optional state parameters #45

jonthanon opened this issue Dec 12, 2018 · 0 comments
Assignees

Comments

@jonthanon
Copy link

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.

@kube kube pinned this issue May 5, 2019
@kube kube self-assigned this Apr 5, 2020
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

No branches or pull requests

2 participants