Answer the question
In order to leave comments, you need to log in
How is type casting done correctly in TypeScript?
Let's say there is a function that can return either SomeType1 or SomeType2.
Inside this function, an Http request is made and JSON is returned.
Everything seems to be fine, but until I try to access the class methods or execute instanceof...
Since the types are decorative in nature and no actual casting is performed.
How to do type casting? How to distinguish one type of returned object from another?
For example here:
getType(id: number) {
return this._httpClient.get<Type|Error>(`${this.apiUrl}/${id}`, { withCredentials: true });
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question