2
lorentz
3y

Typescript is my new favorite and my grudge is the stupid scoping of type assertions. I have an async function that checks whether a variable is set and awaits a change event if it's undefined. This function is working javascript but invalid according to typescript, because it relies on the exact type changing while the function is running. I had to convert it to a mess of promises to bypass this because (and this is the best) the callback-based syntax of identical meaning will reset all type assertions, even locals that are never written after the callback's creation.

Comments
  • 1
    Example? There's probably an easier way.
  • 0
    Type narrowing?
  • 0
    Guys let him rant.
  • 1
    @SortOfTested I had something like this:
  • 0
    @homo-lorens
  • 1
    @bagfox That's the word I was looking for.
  • 2
    @homo-lorens
    It's kind of loosey goosey in the promise X|Y scenario. Personally, I think you'd get a lot more mileage out of the RxJs WebSocketSubject. Among other things it has the buffer to handle all the conditions you're already using.

    In this example, you create a websocket connection. You send A messages, receive B|C messages.

    B and C messages have individual streams that anyone with the service can observe, there's a separate stream for service errors.

    If you wanted to you can also expose a railway by just creating a property that is result of calling asObservable on the subject. Consumers can specify their own behavior by simply calling subscribe

    https://stackblitz.com/edit/...
  • 0
    @SortOfTested Why did I not know about RxJS, this is great.
Add Comment