Skip to content
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

refactor(jest-mock)!: rename and clean up utility types #12435

Merged
merged 12 commits into from
Feb 23, 2022
Merged

refactor(jest-mock)!: rename and clean up utility types #12435

merged 12 commits into from
Feb 23, 2022

Conversation

mrazauskas
Copy link
Contributor

Summary

Split from #12425

While working on Mocked* utility types, I noticed that there is a need to clean up generic utility types used in jest-mock. It made sense to split this to separate PR.

Few useful utilities were copied in from other libraries (which is very good, of course!) and this created some duplication. For instance NonFunctionPropertyNames and FunctionPropertyNames are just the same as PropertyKeysOf and MethodKeysOf. Took me some time to realise this, indeed generics are hard to read by nature. So it seemed helpful to simplify names of these types: MethodKeys, PropertyKeys. Similar to TS builtins Parameters, ConstructorParameters.

Two more renames seemed reasonable: ClassLike, FunctionLike instead of Constructable , MockableFunction.

Finally I used TS builtins Parameters and Awaited instead of custom ParametersOf and Unpromisify. (At the moment ConstructorParameters builtin does not work, but I will figure it out later.)

Test plan

Type tests are added.

@mrazauskas mrazauskas marked this pull request as draft February 20, 2022 19:25
Comment on lines +35 to +37
export type ConstructorLike = {new (...args: Array<any>): any};

export type MethodLike = (...args: Array<any>) => any;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly these don’t work with unknown. Type tests are failing. Assignably should be the reason: "any and unknown are the same in terms of what is assignable to them, different in that unknown is not assignable to anything except any. Also TS is using any in similar util types: https://github.com/microsoft/TypeScript/blob/78818e03908a6cca779fec1355744ed60bda2c63/lib/lib.es5.d.ts#L1526

Comment on lines +39 to +47
export type ConstructorLikeKeys<T> = {
[K in keyof T]: T[K] extends ConstructorLike ? K : never;
}[keyof T];
export type PropertyKeysOf<T> = {
[K in keyof T]: T[K] extends MockableFunction ? never : K;

export type MethodLikeKeys<T> = {
[K in keyof T]: T[K] extends MethodLike ? K : never;
}[keyof T];

export type ArgumentsOf<T> = T extends (...args: infer A) => any ? A : never;
export type PropertyLikeKeys<T> = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternative names: ConstructorKeys<T>, MethodKeys<T>, PropertyKeys<T>. The first one did not sound. Including Like is somewhat more clear. These utils return a tuple of keys with respectively constructor-like, method-like or property-like values of object T.

@mrazauskas mrazauskas marked this pull request as ready for review February 23, 2022 10:49
[K in keyof T]: T[K] extends MockableFunction ? K : never;
export type ConstructorLike = {new (...args: Array<any>): any};

export type MethodLike = (...args: Array<any>) => any;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use CallableFunction from ts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting idea. I was checking if CallableFunction (also NewableFunction) could work here, but it didn’t. Seems like these are great types for bind, apply, but what we need here is just to check that type has callable signature. That’s why adding Like looked good, but it also might be good idea to name them simply: Callable and Constructable. What you think?

packages/jest-mock/src/index.ts Show resolved Hide resolved
@SimenB SimenB merged commit a653a6f into jestjs:main Feb 23, 2022
@mrazauskas mrazauskas deleted the refactor-mock-utilities branch February 23, 2022 13:59
@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants