I've been struggling to declare the TypeScript types of an initial state of a React Context with our errors. It'll store a list of items, and the initial state should be an empty array.
I've tried many options like using an interface instead of a type, using an object to declare the types of items and setItems, declaring the type exactly as it's inferred, even trying to bypass de type declaration altogether, and I've finally managed to make it work like this:
type Props = [any, any];
export const ListContext = createContext<Partial<Props>>([]);
export const ListProvider = (props: any) => {
const [items, setItems] = useState([]);
return (
<ListContext.Provider value={[items, setItems]}>
{props.children}
</ListContext.Provider>
);
};
I'm not sure if it's the correct way (using any doesn't look like it), and I don't fully understand why it works either.
How can I improve it? Thanks in advance.