You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
The instrument function currently claims to return the same type as it receives. However, that's not the case when intercept is set to true and a Promise is returned instead.
This causes issues when trying to await things like userEvent.click(...) in @storybook/testing-library, as reported in storybookjs/testing-library#10.
I believe the typings here could be improved by defining a recursive conditional type. That would look something like this:
/** * A utility type that transforms a function type into one that returns a * Promise type of the existing value type. */typePromisifiedReturn<Textends(...args: any)=>any>=ReturnType<T>extendsPromiseLike<unknown>
? T
: (...a: Parameters<T>)=>Promise<ReturnType<T>>/** * A recursive utility type that transforms all functions types nested within * the object to return Promises instead of their existing value types. */typeInstrumentedObject<T>={[KinkeyofT]:
// If the value is a function, we transform its type to be one that returns a PromiseT[K]extends(...args: any)=>any
? PromisifiedReturn<T[K]>// If it's an Array, we recursively transform its elements' types
: T[K]extends(infer U)[]
? InstrumentedObject<U>[]// If it's an Object, we recursively transform its properties' types
: T[K]extendsobject
? InstrumentedObject<T[K]>// Otherwise, we just keep the type as it is
: T[K]}
You can explore the proposed solution at this TypeScript playground link and hover over the instrumented object's properties to see what there types look like after instrumentation.
Note that if the behavior should be that the return type should remain TObj if instrument is not set to true, that can be achieved by a discriminant function signature that returns a different type depending on the input.
To Reproduce
Try to await userEvent.click(...) in a story and see linter warnings that you are awaiting a non-promise.
We’re cleaning house! Storybook has changed a lot since this issue was created and we don’t know if it’s still valid. Please open a new issue referencing this one if:
Describe the bug
The
instrument
function currently claims to return the same type as it receives. However, that's not the case whenintercept
is set totrue
and aPromise
is returned instead.This causes issues when trying to
await
things likeuserEvent.click(...)
in@storybook/testing-library
, as reported in storybookjs/testing-library#10.I believe the typings here could be improved by defining a recursive conditional type. That would look something like this:
You can explore the proposed solution at this TypeScript playground link and hover over the instrumented object's properties to see what there types look like after instrumentation.
Note that if the behavior should be that the return type should remain
TObj
ifinstrument
is not set totrue
, that can be achieved by a discriminant function signature that returns a different type depending on the input.To Reproduce
await userEvent.click(...)
in a story and see linter warnings that you are awaiting a non-promise.System
Additional context
N/A
The text was updated successfully, but these errors were encountered: