Typing custom hooks
I made a custom hook for a post of a file, this is the code:
How can i type it so i don't have typescipt errors when i use this parts ?
This is how i use it in the components:
And the use them like
6 Replies
also
why the hell are you using a tuple with 5 items 😅
because maybe i don't need to use all of them idk
@thetarnav why this works and without typing "as const" typescript shows an error?
you would do it with an object ? why don't you like the array ?
as const
will make the shape readonly, and so if the size of the array can’t change, and it’s known at compile time, it’s pretty much a tuple
if you remove as const
, ts will type it as it could change, so the type will be very broad.
also tuples are usually used when there are few items, and it’s obvious what they are
like [get, set]
or [result, error]
but if you have a lot of fields, you have lookup what they are in each order
tuples are mostly used so you can easily rename items@thetarnav so you would return as an object i imagine no?
like this?
as const is not needed here
objects are assumed to have static shape by default
i see