'How to fetch with method GET And Type script
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
Solution 1:[1]
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;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Robin-Hoodie |
