In my custom hook, I utilize the axios
library for making requests:
const useCustomHook = ({ endPoint = "", method = "GET", options = {} }) => {
const [data, setData] = useState([]);
const [request, setRequest] = useState<AxiosRequestConfig>({
url: endPoint,
method,
headers: {},
data: options
});
{...}
}
The Axios types (AxiosRequestConfig) define that method
should adhere to type Method
:
type Method =
| 'get' | 'GET'
| 'delete' | 'DELETE'
| 'head' | 'HEAD'
| 'options' | 'OPTIONS'
| 'post' | 'POST'
| 'put' | 'PUT'
| 'patch' | 'PATCH'
However, an error is triggered when dealing with method
:
Type 'string' is not assignable to type '"GET" | "get" | "delete" | "DELETE" | "head" | "HEAD" | "options" | "OPTIONS" | "post" | "POST" | "put" | "PUT" | "patch" | "PATCH" | undefined'.
https://i.sstatic.net/JbNvl.png
I could simply declare method
as a string
, but that would compromise the desired type safety.