question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IPromiseBasedObservable typings improvement

See original GitHub issue

As for now, IPromiseBasedObservable<T> has this definition:

export interface IPromiseBasedObservable<T> {
    value: T;
    state: PromiseState;
    reason: any;
    promise: PromiseLike<T>;
    case<U>(handlers: {pending?: () => U, fulfilled?: (t: T) => U, rejected?: (e: any) => U}): U;
}

I suggest to change that to get stronger types:

interface IPromiseBasedObservablePending {
  state: "pending"
}
interface IPromiseBasedObservableFulfilled<T> {
  state: "fulfilled"
  value: T
}
interface IPromiseBasedObservableRejected {
  state: "rejected"
  value: Error
}
export type IPromiseBasedObservable<T> = (
  IPromiseBasedObservablePending |
  IPromiseBasedObservableFulfilled<T> |
  IPromiseBasedObservableRejected
) & {
  promise: PromiseLike<T>;
  case<U>(handlers: {pending?: () => U, fulfilled?: (t: T) => U, rejected?: (e: Error) => U}): U;
}

After that typescript will force developer to cover all cases of state and gives strict access to value:

function render(v: IPromiseBasedObservable<string>): string {
  return v.value // Property 'value' does not exists on type 'IPromiseBasedObservable<string>'
}

function render(v: IPromiseBasedObservable<string>): string {
    if (v.state === "pending") {
        return "value is pending"
    }
    if (v.state === "rejected") {
        return `value is rejected with: ${v.value.message}` // v.value is Error here
    }
    return `value is: ${v.value}`; // v.value is string here
}

Also I suggest to either:

  1. Remove case function from IPromiseBasedObservable
  2. Make all handlers required, for each state.

With strictNullChecks enabled it gives proper check for all cases of v.state are covered. It is breaking change and usable only for TS2+, so, it should introduce new major version.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mweststratecommented, Jun 13, 2017

Seems like a good improvement @Strate. Feel free to implement|!

0reactions
Stratecommented, Jul 25, 2017

Wow, great! Thank you, and sorry for not to be volunteer of this 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

IPromiseBasedObservable defaults to observable deep false
From the description it sounds like the value should be observable and I expect it to react to changes to its "deeper" properties....
Read more >
mobx-utils - npm
Returns IPromiseBasedObservable<T> ... Alternative syntax for async actions, similar to flow but more compatible with Typescript typings.
Read more >
How to use IPromiseBasedObservable in mobx-utils - Tabnine
Best JavaScript code snippets using mobx-utils.IPromiseBasedObservable(Showing top 4 results out of 315) ; value && new AdminTabManager(this.promise.value) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found