I am trying to use the Fetch API in TypeScript, but for some reason I am getting
Type 'string' is not assignable to type 'RequestMode'.
Here is my code
export class MainService{
constructor(){}
request(requestObj){
if (!requestObj.url) {
return 'Please provide the end point url'
}
else {
let requestParam = {
method: requestObj.method ? requestObj.method : 'GET',
headers: new Headers(),
mode: 'cors'
};
fetch(endPointHost+requestObj.url,requestParam).then(function(resp){
//^^^ error thrown here
resp.json();
})
}
console.log(" Reached request");
}
}
The error reported is
TS2345: Argument of type '
{ method: any; headers: Headers; mode: string; }
' is not assignable to parameter of type 'RequestInit
'. Types of property 'mode
' are incompatible. Type 'string
' is not assignable to type 'RequestMode
'.