Skip to content

produce causes TS error #732

Closed
Closed

Description

@bgoscinski

Describe the bug
Valid usage of produce causes TS error.

To Reproduce
Provide a code snippet and steps to reproduce the behavior:

  1. Go to playground
  2. Wait for code to be type checked
  3. See the error

Expected behavior
No error

Reproduction
See above

Additional context
The error is silenced in the tutorial by // @ts-nocheck directive: https://www.solidjs.com/tutorial/stores_mutation?solved

Activity

  1. added
    typescriptrelating to typescript or types
    PRs WelcomeAn issue which might not be prioritized right away but would be open to community contribution
    on Nov 16, 2021
  2. mac-ignacio commented on Nov 26, 2021

    @mac-ignacio

    It's not actually an error. You are just missing some interface for produce. Add a Count interface and provide it as a generic type for produce like the code below.

    interface Count {
      count: number;
    }
    
    function Counter() {
      const [store, setStore] = createStore({ count: 0 });
    
      const increment = () => {
        setStore(
          produce<Count>((s) => {
            s.count++;
          })
        );
      };
      return (
        <button type="button" onClick={increment}>
          {store.count}
        </button>
      );
    }
  3. bgoscinski commented on Nov 26, 2021

    @bgoscinski
    Author

    Thanks @mackignacio!
    While your solution works I assumed that the original intent was that explicit interface should not be necessary because TS should be able to infer the types. This would greatly improve the ergonomics of produce.

    @ryansolid is my assumption correct?
    In any case please treat this ticket as a request for making the generic optional (inferable) :)

    I tried to dig into the types of produce and setStore and fix it myself but oh boy they're complex lol

  4. davedbase commented on Nov 26, 2021

    @davedbase
    Member

    @trusktr has been reviewing our types maybe he has an opinion on this. Also lol...wait until you see some of the new TS types coming. Weeeee

  5. mac-ignacio commented on Nov 27, 2021

    @mac-ignacio

    @trusktr I can help with the types if needed. Just show me where to start.

  6. trusktr commented on Dec 12, 2021

    @trusktr
    Contributor

    Indeed there are more places to take a stab at improving inference, plus making the source code itself more type safe. Just run Go To Definition in VS Code on produce and that'll take you right to it.

  7. added 12 commits that reference this issue on Dec 13, 2021
    1b5e244
    88d1020
    9ec867d
    95d063c
    4826b9e
    8b29aee
    7447086
    f0e8b79
    8fe9da6
    f97f3f3
    3222679
    171468e
  8. added a commit that references this issue on Jan 6, 2022
    bdf39a7
  9. added a commit that references this issue on Nov 12, 2025
    c03923e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    PRs WelcomeAn issue which might not be prioritized right away but would be open to community contributiontypescriptrelating to typescript or types

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions