I write a custom hook for handle input in my app. I use generic type to define what type return for my custom hook. Here is my code
interface IUseInput<T, U> {
(): [T, (e: ChangeEvent<HTMLInputElement>) => void, U];
}
function useInput<T, U>(): IUseInput<T, U> {
const [input, setInput] = useState({});
const [isDirty, setDirty] = useState({});
const handleInputChange = (e: ChangeEvent<HTMLInputElement>) => {
setInput({
...input,
[e.target.name]: e.target.value
});
setDirty({
...isDirty,
[e.target.name]: true
});
};
return [input, handleInputChange, isDirty] //this line get error
}
const Component: React.FC = () => {
const [input, handleChangeInput, isDirty] = useInput<{username: string, password: string}, {username: boolean, password: boolean}>();
return (
....
)
}
But I get this error Type '{}[]' is not assignable to type 'IUseInput<T, U>
WHere is my wrong? Please help me