2

I'm true beginner with type script can you help me with this simple fetch

this is function

export async function askForList(){
   return await fetch('http://127.0.0.1:3333/applist').then((res) => res.json()) 
}

this is expected data

interface RnMcharacter{ 
    id: number,
    img: string,
    name: string,
    number: number
};

I'v tried many combination but moast common error is ERROR in src/transmission/apiserv.ts:27:4 @typescript-eslint/no-unsafe-return: Unsafe return of an any typed value.

Can you show me example of proper function so I can understad what mi doing wrong. Thx

2
  • 2
    Does this answer your question? Typescript Unsafe return of an `any` typed value Commented Mar 23, 2022 at 19:30
  • 1
    You can add the type after the method name: function askForList(): RnMcharacter { Commented Mar 23, 2022 at 19:36

1 Answer 1

4

You can type the data prop as RnMcharacter. You can also remove the then call as you're using async|await

export async function askForList(){
  const res = await fetch('http://127.0.0.1:3333/applist');
  const { data }: { data: RnMcharacter } = await res.json();
  return data;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! it really helped me. I'v fixed one more bug in place where I call the function and now is grate.

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.