I have the following code in TypeScript React:
const AirQuality= async ():Promise<string>=>{
console.log('Air Quality')
let data = await fetch(url,{
method:'GET',
// mode:'no-cors'
})
return data.json()
}
As you can see here, data.json()
was returned from the function. The thing I don't get is why I am not getting an error when I define the Promise type as string. What is the issue here? I changed Promise type to number, still, no error is shown. What could be the cause for that?
CodePudding user response:
json()
parses the returned JSON into a JavaScript object structure. Since this can be any kind of object depending on what the server returns, it has the type Promise<any>
.
An any
type can be converted to anything including number or string, which is why you don't see an error.