jeffijoe / mobx-task

Makes async function state management in MobX fun.
MIT License
238 stars 6 forks source link

Typescript support #8

Closed AlexCppns closed 5 years ago

AlexCppns commented 6 years ago

Hi there,

Right now, we have to manually implement index.d.ts in our project and I cannot figure out how to declare the decorator @task with options so that it works in typescript. My current workaround is

// @ts-ignore
@task({ swallow: true })

our current index.d.ts file:


declare module "mobx-task" {
  type NoArgWorker<U> = () => U
  type OneArgWorker<T1, U> = (a: T1) => U
  type TwoArgWorker<T1, T2, U> = (a: T1, b: T2) => U
  type ThreeArgWorker<T1, T2, T3, U> = (a: T1, b: T2, c: T3) => U

  interface TaskStatusAware<U> {
    match: any

    result: U

    pending: boolean
    resolved: boolean
    rejected: boolean
  }

  interface NoArgTask<U> extends TaskStatusAware<U> {
    (): U
  }

  interface OneArgTask<T1, U> extends TaskStatusAware<U> {
    (a: T1): U
  }

  interface TwoArgTask<T1, T2, U> extends TaskStatusAware<U> {
    (a: T1, b: T2): U
  }

  export function task<U>(worker: NoArgWorker<U>, options?: Object): NoArgTask<U>
  export function task<T1, U>(worker: OneArgWorker<T1, U>, options?: Object): OneArgTask<T1, U>
  export function task<T1, T2, U>(
    worker: TwoArgWorker<T1, T2, U>,
    options?: Object
  ): TwoArgTask<T1, T2, U>

}

mobx packages from package.json:

    "mobx": "^4.1.1",
    "mobx-react": "^5.0.0",
    "mobx-react-router": "^4.0.2",
    "mobx-task": "^1.0.0-alpha.0",
jeffijoe commented 6 years ago

Hi Alex, I'm afraid I can't be of much help. I wanted to write mobx-task in TS but the decorator pattern is not well supported in TS so I didn't bother.

I might take a look at it once I have some more time to dabble in the type system.

JulianWielga commented 5 years ago

I've made PR to DefinitelyTyped with types. https://github.com/DefinitelyTyped/DefinitelyTyped/pull/34690

jeffijoe commented 5 years ago

Great work @JulianWielga !