I
I
iluxa18102019-04-11 13:54:54
typescript
iluxa1810, 2019-04-11 13:54:54

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 });
  }

That is, at the output I get either Type or Error. Is it really necessary to sort through the fields with handles, and then do Object.Assing in order to get a real type and instanteOf and methods start working correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
irishmann, 2019-04-11
@irishmann

Type conversion

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question