Is it necessary to set the return value of the function in Angular?

such as

getHeroes (): Observable < Hero [] > {
return this.http.get < Hero [] > (this.heroesUrl)
}

can I just write it this way?

getHeroes () {
return this.http.get < Hero [] > (this.heroesUrl)
}

is it necessary to give < Hero [] > after get? What"s the point?

I see that some code will get the data of the json class from an api
and then write directly:

private headers = new HttpHeaders (). Set ("Content-Type"," application/json");
/ / does not provide the return of the function and does not give anything after the get
getHeroes () {
return this.http.get (this.myurl, {headers:this.headers})
}

is it because the data types to be accepted are not defined in angular in advance?

Thank you for answering!

Feb.26,2021

to talk about this in detail, you may need to know what weakly typed languages and strongly typed languages are. Google it yourself

to put it simply, specifying the type helps to be tool-friendly, and development tools can use definitions for real-time detection and IntelliSense, giving feedback during the code writing phase. For example, when developing typescript with vs code, enter the specified type of variable. Type member perception at symbol

Menu