I'm trying to type a custom React hook:
import { useRef } from 'react';
type Reference = [
Promise<unknown>,
(value?: unknown) => void,
(reason?: unknown) => void,
];
const usePromise = () => {
const reference: Reference = [];
const container = useRef(reference);
reference[0] = new Promise((resolve, reject) => {
reference[1] = resolve;
reference[2] = reject;
});
// [promise, resolve, reject]
return container.current;
};
export default usePromise;
TypeScript complains about reference
saying:
Type '[]' is not assignable to type 'Reference'.
Source has 0 element(s) but target requires 3.
How can I allow TypeScript to accept the empty array / tuple initialization, too? And also maybe there is a way to give usePromise
the type of the value, too, so that it doesn't say unknown
?