-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(@jest/types): infer argument types passed to test
and describe
functions from each
tables
#12885
Conversation
timeout?: number, | ||
) => void; | ||
|
||
<T extends readonly [unknown, ...Array<unknown>]>(table: ReadonlyArray<T>): ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An array of arrays is actually treated as array of tuples. Spreading ['abc', 123]
as tuple gives a type a: string, b: number
, spreading the same thing as an array results in a type a: string | number, b: string | number
. The hard part was to capture indefinite number of tuple members. [unknown, ...Array<unknown>]
looks clumsy, but does this job well.
timeout?: number, | ||
) => void; | ||
|
||
<T extends ReadonlyArray<unknown>>(table: ReadonlyArray<T>): ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a table is defined a variable (not inline), TypeScript treats it as Array<Array<...>>
. This overload will catch this case.
timeout?: number, | ||
) => void; | ||
|
||
<T extends Record<string, unknown>>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finally this is here to allow typing template string literals through generic argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
woah, this is awesome! 👏
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
This PR is filling one more gap of types. Currently types of arguments passed from
each
tables totest
anddescribe
functions areany
. In this field,@types/jest
does better job. I was playing with for some time. The solutions is different than the one from DT – somewhat better, but with some limitations. Documentation noting the caveats is included (by the way, types from DT has similar issues).Probably I will have to revisit it later, but that is a strong start already.
Test plan
The detailed type tests for
each
now live in a separate file. The type tests for global API were cleaned up and slightly extended. Could not leave them. Apologies for many tests. Just wanted to be sure that everything works.