refactor(jest-mock)!: rename and clean up utility types#12435
refactor(jest-mock)!: rename and clean up utility types#12435SimenB merged 12 commits intojestjs:mainfrom mrazauskas:refactor-mock-utilities
Conversation
| export type ConstructorLike = {new (...args: Array<any>): any}; | ||
|
|
||
| export type MethodLike = (...args: Array<any>) => any; |
There was a problem hiding this comment.
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
| 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> = { |
There was a problem hiding this comment.
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.
| [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; |
There was a problem hiding this comment.
can we use CallableFunction from ts?
There was a problem hiding this comment.
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?
|
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
Split from #12425
While working on
Mocked*utility types, I noticed that there is a need to clean up generic utility types used injest-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
NonFunctionPropertyNamesandFunctionPropertyNamesare just the same asPropertyKeysOfandMethodKeysOf. 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 builtinsParameters,ConstructorParameters.Two more renames seemed reasonable:
ClassLike,FunctionLikeinstead ofConstructable,MockableFunction.Finally I used TS builtins
ParametersandAwaitedinstead of customParametersOfandUnpromisify. (At the momentConstructorParametersbuiltin does not work, but I will figure it out later.)Test plan
Type tests are added.