Skip to content

Commit

Permalink
fix: Make types of asyncComputed describe the value for pending more …
Browse files Browse the repository at this point in the history
…accurately
  • Loading branch information
Iku-turso committed May 17, 2023
1 parent 79611a8 commit e4d6325
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packages/mobx-utils/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { IComputedValue } from 'mobx';

export type IAsyncComputed<T> = {
value: IComputedValue<T>;
export type IAsyncComputed<TValue, TPending> = {
value: IComputedValue<TValue | TPending>;
pending: IComputedValue<boolean>;
invalidate: () => void;
};

type AsyncComputedParams<T> = {
getValueFromObservedPromise: () => Promise<T>;
valueWhenPending?: T;
type AsyncComputedParams<TValue, TPending> = {
getValueFromObservedPromise: () => Promise<TValue>;
valueWhenPending?: TValue | TPending;
betweenUpdates?: 'show-pending-value' | 'show-latest-value';
};

export function asyncComputed<T>(
configuration: AsyncComputedParams<T>,
): IAsyncComputed<T>;
export function asyncComputed<TValue, TPending = undefined>(
configuration: AsyncComputedParams<TValue, TPending>,
): IAsyncComputed<TValue, TPending>;
53 changes: 53 additions & 0 deletions packages/mobx-utils/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { expectType } from 'tsd';
import { asyncComputed } from './index';
import { IComputedValue } from 'mobx';

// asyncComputed, given showing latest value and specified pending value, typing is ok.
const instance1 = asyncComputed({
getValueFromObservedPromise: () => Promise.resolve('some-value'),
valueWhenPending: 42,
betweenUpdates: 'show-latest-value',
});

expectType<{
value: IComputedValue<string | number>;
invalidate: () => void;
pending: IComputedValue<boolean>;
}>(instance1);

// asyncComputed, given showing latest value but no specified pending value, typing is ok.
const instance2 = asyncComputed({
getValueFromObservedPromise: () => Promise.resolve('some-value'),
betweenUpdates: 'show-latest-value',
});

expectType<{
value: IComputedValue<string | undefined>;
invalidate: () => void;
pending: IComputedValue<boolean>;
}>(instance2);

// asyncComputed, given showing pending value between updates and specified pending value, typing is ok.
const instance3 = asyncComputed({
getValueFromObservedPromise: () => Promise.resolve('some-value'),
valueWhenPending: 'some-initial-value',
betweenUpdates: 'show-pending-value',
});

expectType<{
value: IComputedValue<string>;
invalidate: () => void;
pending: IComputedValue<boolean>;
}>(instance3);

// asyncComputed, given showing pending value between updates, but no specified pending value, typing is ok.
const instance4 = asyncComputed({
getValueFromObservedPromise: () => Promise.resolve('some-value'),
betweenUpdates: 'show-pending-value',
});

expectType<{
value: IComputedValue<string | undefined>;
invalidate: () => void;
pending: IComputedValue<boolean>;
}>(instance4);
1 change: 1 addition & 0 deletions packages/mobx-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"scripts": {
"build": "ogre-tools-build-js",
"test": "ogre-tools-test",
"test:types": "tsd",
"code-style:verify": "ogre-tools-verify-code-style",
"code-style:fix": "ogre-tools-fix-code-style"
},
Expand Down

0 comments on commit e4d6325

Please sign in to comment.