solidjs / solid

A declarative, efficient, and flexible JavaScript library for building user interfaces.
https://solidjs.com
MIT License
32.37k stars 925 forks source link

Typescript types of `mutate` don't allow unsetting a value #1000

Closed cstrnt closed 2 years ago

cstrnt commented 2 years ago

Describe the bug

When using createResource I sometimes want to call mutate(null) or mutate(undefined) to optimistically remove an item. The current typescript types only allow calling mutate with T

Your Example Website or App

https://playground.solidjs.com/?version=1.4.1#NobwRAdghgtgpmAXGGUCWEwBowBcCeADgsrgM4Ae2YZA9gK4BOAxiWGjIbY7gAQi9GcCABM4jXgF9eAM0a0YvADo1aAGzQiAtACsyAegDucAEYqA3EogcuPfr2ZCouOACU4dJqymz5ilXQa2noWVlYy9BDMuGi0ELwAwgwQLowAFACU-Fa8DnFkfMAw+O5knixwWPYw9LjOcFIAurwAvA5OLqUMFWk5ucp4HrgqWH25ma0AfLwACn5oZHAAdEKBAG5wvYMFKhmj8f0CGGgxUGoAamf0cIgDLjtgUn0ZlhB9QrhM8b0HuQA8kzG-T+ZEIUAgkxAxVK5VYmUkf30oPBgN+wJMtVwcV4cQSGmYAGsWiAJi1pjU6i40hB6Go1BlJJMALK1eqIjG4LEQoGI1G5F5WSRhCBCUTiNKk6Z-JKRVK8fSTKoiWjMejwFJLADmcFwAFE1HB1bgAEL4ACSIi2UEIhF2AEIBZhJI0gA

Steps to Reproduce the Bug or Issue

Open the solid playground with the given URL

Expected behavior

As a user I want to be able to call mutate with null or undefined without Typescript errors

Screenshots or Videos

No response

Platform

Additional context

No response

otonashixav commented 2 years ago

This is how I'd expect the types to be inferred. If you want null or undefined in the types then you should explicitly type the promise or type-cast the return type of the promise to null | string.

https://playground.solidjs.com/?hash=-313264341&version=1.4.1

cstrnt commented 2 years ago

You’re right, sorry for the inconvenience