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 error when update store use functioin filter #656

Closed weijarz closed 3 years ago

weijarz commented 3 years ago

To Reproduce

Code snippet from: https://www.solidjs.com/docs/latest/api#updating-stores

const [state, setState] = createStore({
  todos: [
    { task: 'Finish work', completed: false },
    { task: 'Go grocery shopping', completed: false },
    { task: 'Make dinner', completed: false }
  ]
});
setState('todos', todo => todo.completed, 'task', t => t + '!')

Expected behavior

The example code copied from Solid document will cause typescript error on filter function.

Reproduction

https://playground.solidjs.com/?version=1.1.1#NobwRAdghgtgpmAXGGUCWEwBowBcCeADgsrgM4Ae2YZA9gK4BOAxiWGjIbY7gAQi9mjOFFxwAyrm5xeAX14AzRrRi8AOjVoAbNABMAtACsyAejJThGgNxqIt5rQjnewc6LhZeZOLknuAurwAvILC7pLSABQgtry8Urq0ZIgusXH88VBkANYpAOQAYhhoZAAWvADu3Nl5ng6cWj5wuikKUFreclhpcQK4Wbm8eQDitLwA5sqsjPhepbSEhBjjtYIqhI1iLYrtnbLdEOkZ-Tn5ALJQ2TK6GBBwjKv1G03bbR0ysmn+trIAlDZOHx+MSRPIJJKrcHBAB88VoiQAdE9Ns1PGCBpCYfFeABqIYAQjyv1sYFk-iAA

fictitious commented 3 years ago

For TypeScript, all the functions passed to setState, except the last one, need explicit types for their parameters.

There's no error if todo parameter type is spelled out:

 setState('todos', (todo: {task: string; completed: boolean}) => todo.completed, 'task', t => t + '!')

The code in Solid examples is javascript. TypeScript type inference has its limits. Here there are multiple parameters with inter-dependent types which it can't figure out without some help.

ryansolid commented 3 years ago

As far as I know @fictitious is correct. These are very difficult types and have been the effort of many of the community to get right. There are still gaps though. So I'd welcome anyone who has the knowledge to look at this but realistically this is probably beyond my or any of the team member's current TypeScript knowledge to address.

ryansolid commented 3 years ago

Type improvements were made and query answered. Closing.