1

I have this code - where I'm trying to pass in a string title:

let GetData: (title: string) => Promise<APIStates<Series[]>>;

if (process.env["REACT_APP_SERVICE_VERSION"] === "development") {
    GetData = useGetDataServiceDummy(title)
}

However I keep getting 2 errors and can't figure out the syntax to resolve it:

  • Cannot find name 'title'
  • Promise<APIStates<Series[]> is not assignable to (title: string) => Promise<APIStates<Series[]>

Could someone please help me with the syntax to get around this error.

And for reference useGetDataServiceDummy

export async function useGetDataServiceDummy(title: string): Promise<APIStates<Series[]>> {
    // do soemthing with `title`
    return { status: "loaded", payload: dummyData };
}

Any help would be appreciated.

Thanks.

3
  • You've defined GetData as a function, but assigning to it the promise (the function's return value) Commented Sep 10, 2019 at 8:09
  • Regarding title - in shown part of code it is not defined.. Commented Sep 10, 2019 at 8:15
  • 1) You're saying useGetDataServiceDummy will return a Promise and it just returns a regular Object. 2) title is literally not defined in the snippet you provided 3) You're assigning a return type of a useGetDataServiceDummy to GetData by calling it .(title) and you said it will be a function and not a Promise => : (title: string) => Promise<APIStates<Series[]>> Commented Sep 10, 2019 at 9:17

2 Answers 2

2

It looks like you're trying to assign useGetDataServiceDummy to GetData, however what you're actually doing is calling useGetDataServiceDummy and then assigning the return value to GetData. Try this instead:

if (process.env["REACT_APP_SERVICE_VERSION"] === "development") {
    GetData = useGetDataServiceDummy
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yep - think that was it - I didn't need useGetDataServiceDummy(title)
Although if I'm being fully honest, I don't understand how title gets passed to useGetDataServiceDummy`.
at some later point ` GetData` will get called with a title. Since you've assigned userGetDataServiceDummy to GetData that means the dummy function will be called.
0

If you want to assign useGetDataServiceDummy(title) function to GetData you should change your definition of GetData like this;

let GetData: Promise<APIStates<Series[]>>;

Comments

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.