2

Say you have a function that returns the following:

const useHook0 = (props) => {
    ...
    API call
    ...
    return [varA, varB, varC] as [boolean, boolean, any]
}

And then you wish to use it in another function like so:

const [itemA, itemB, itemC]: [boolean, boolean, number] = useHook0(configA);
const [itemA1, itemB1, itemC1]: [boolean, boolean, string] = useHook0(configB);

Is there a way to apply types to the original array, say in the form of an interface, such that you only need to indicate what type the 3rd constant will be?

In other words, I feel that writing : [boolean, boolean, number] or : [boolean, boolean, string] is redundant and unnecessary, but I'm unable to find a more concise way of doing this.

EDIT:

The reason why it is being constructed in that manner is that function0 is a hook that is making API calls. So the data can be in many different forms.

The reason why I am not providing it in the form of an object is that if I have multiple API calls being made within one function, I need the ability to rename the constants.

EDIT 2:

The intent in avoiding an assertion each time is to make it such that when itemA.map(result => ...) is called 5 times, I don't need to write itemA.map((result: string) => ...) every single time. Just trying to follow DRY.

3
  • Are you casting the type or trying to enforce a return type at the function level? Commented Dec 19, 2019 at 20:23
  • Seems like it would be easier to just use an object with three properties, but whatever. Can that third slot truly be any? Or are the types actually limited? You could just make the type be [boolean, boolean, string | number]... Commented Dec 19, 2019 at 20:23
  • Edited post to answer your questions Commented Dec 19, 2019 at 20:30

1 Answer 1

2

If you know what the different configs should return, you can make use of Generics to solve this:

const useHook0 = <T>(props): [boolean, boolean, T] => {...}
const [itemA1, itemB1, itemC1] = useHook0<number>(configA)
const [itemA1, itemB1, itemC1] = useHook0<string>(configB)
Sign up to request clarification or add additional context in comments.

5 Comments

I edited the post, but the intent is to make it so that when the constants are declared by calling the function, the types are defined such that if I were to call .map() on a constant it would have a type.
I went ahead and updated my answer and removed the previous parts for clarification. Hope that helps!
Yup it works! Thank you so much! Actually if you want to edit your answer to remove the type assertion after the constant declarations, that's actually all I need to have written on my end. So basically "const [itemA1, itemB1, itemC1] = useHook<number>(configA);"
Removed. Thanks @anesthetic.
The extra <number> might not even be needed if the type of itemC1 is already known

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.