Variable 'newData' is used before being assigned - TypeScript
Clash Royale CLAN TAG#URR8PPP
Variable 'newData' is used before being assigned - TypeScript
I am getting error in the below implementation of typescript code. I am mapping here one type to another. But vscode shows error that variable 'newData' is used before being assigned. I know it may be a silly error, but I am unable to find it.
onKeyUp(value: string)
console.log(value);
var link = ``
link = `$API/$value`
this.hn.getNews().subscribe(data =>
this.loading = false;
var newData:NewsItem;
fetch(link)
.then(function(response)
return response.json();
)
.then(function(myJson)
for (var i = 0; i < myJson.length; i++)
newData.push(myJson[i])
)
this.news = newData;
);
}
1 Answer
1
You need to initialize it to empty array as follows,
var newData:NewsItem = ;
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
thank you so much. I can't believe I missed that. Thank you so much. Have a nice day, sir!
– CodeNinja
Aug 12 at 2:09