-1

I have a function that returns an object of type Bar like this

myFunc() {
   return <Bar>asyncGet(); / fetches the info and casts the server response to Bar
}

than in some controller, if I do

let bar = myFunc();
// bar instanceof Bar doesn't return true...

instanceof is not working as expected since I didn't instanciate bar via Bar constructor.

How can I check that the object returned by myFunc is of type Bar ?

Lev
  • 13,856
  • 14
  • 52
  • 84
  • 3
    The `instanceof` operator is native JavaScript. The Typescript type cast is long forgotten by the time your code runs; it's a compile-time thing. – Pointy Oct 23 '17 at 13:45
  • 1
    You should post your definition of `Bar` and the signature of `asyncGet()`. Otherwise the answers will be general: compile-time type assertions (which TypeScript has) are not the same as run-time type casting (which TypeScript doesn't have); make sure `asyncGet()` returns some kind of object instead of a Promise; use the `Bar` constructor with the object somehow to get an actual instance of `Bar`; and/or write a user-defined type guard to determine if the returned object is compatible with `Bar`, etc. – jcalz Oct 23 '17 at 13:51

2 Answers2

0

Since your call to asyncGet is probably asynchronous - it is most likely returns Promise. Hence you need to either work with it oruse async/await approach. Something like this should watch:

myFunc() {
    return <Bar>asyncGet();
}
myFunc().then(bar => { /* do something with it*/});

or

async myFunc() {
   return <Bar>asyncGet();
}
let bar = await myFunc();
Flying
  • 4,422
  • 2
  • 17
  • 25
  • 1
    That's true, but it probably won't help the OP as the typescript cast doesn't change the actual type of the object as seen by `instanceof`. – Duncan Oct 23 '17 at 13:48
  • I have relatively small experience with TypeScript but both [official docs](https://www.typescriptlang.org/docs/handbook/advanced-types.html#instanceof-type-guards) and [this answer](https://stackoverflow.com/a/24707830/2633956) tells that `instanceof` works well in TypeScript. – Flying Oct 23 '17 at 13:52
  • Yes but `instanceof` will tell you the real type of an object. If `asyncGet()` returns say a json structure that has the fields of a `Bar` the cast will let you assign it to a `Bar` but `instanceof` won't know that you cast it as the real type is still just `object`. – Duncan Oct 23 '17 at 13:58
  • 2
    TypeScript has a problematic relationship with `instanceof`: It is one of the few places in JavaScript where typing is nominal, but TypeScript [only supports structural typing](https://github.com/Microsoft/TypeScript/issues/202). There are [several](https://github.com/Microsoft/TypeScript/issues/8168) open [issues](https://github.com/Microsoft/TypeScript/issues/8316) about this difference. – jcalz Oct 23 '17 at 14:00
-2

You can tell your function what type it has to return with :bar at the end. Example:

myFunc():bar{
   return <Bar>asyncGet(); / fetches the info and casts the server response to Bar    
} 

That way your function has to return of type bar.