I have multiple pages and they all have useState of different types:
//Page 1 ...
const [ShoppingListKey, setShoppingListKey] = useState<keyof ShoppingList>();
//Page 2 ...
const [isTrue, setIsTrue] = useState<boolean>(false);
//Page 3 ...
const [String, setString] = useState<string>('');
So, I have a component, and what I want it to do, is to take the target state value from its parent, and set its parents’ state to the targeted value:
interface ChildProps {
targetStateValue: keyof ShoppingList | boolean | string | undefined;
setStateFunc: Dispatch<React.SetStateAction<keyof ShoppingList | boolean | string | undefined>>
}
export const Child = ({targetStateValue, setStateFunc}: ChildProps) => {
<button OnClick={()=>{setStateFunc(targetStateValue);}}>BUTTON</button>
}
In ShoppingList Parent:
<Child
setStateFunc={setShoppingListKey} //ERROR
targetStateValue={something}
/>
The Error says: Type ‘Dispatch<SetStateAction<keyof ShoppingList | undefined>>’ is not assignable to type ‘Dispatch<SetStateAction<string | boolean | undefined>>’
2
Answers
It is possible that TypeScript has too much inference going on under the hood and and you might just need to simply your types by making a dedicated
type
for your solution.Now you have a dedicated TypeScript
type
for yourtargetStateValue
to use and you can use it as follows:You can use a generic component.
Such component would work over a variety of types rather than a single one.
You define a generic interface for your child props. And a generic component which uses that interface as the type for its props.
Using the above, we can change
T
on invocation/definition and based on that the the internal types of the componet will change.For simplicity i created a simple interface for ShoppingList and also gave a prefilled value to the state variable
ShoppingListKey
. The code looks like below:CodeSandbox