-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Make types of asyncComputed describe the value for pending more …
…accurately
- Loading branch information
Showing
3 changed files
with
62 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters