-
-
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
refactor(jest-mock)!: rename and clean up utility types #12435
Conversation
export type ConstructorLike = {new (...args: Array<any>): any}; | ||
|
||
export type MethodLike = (...args: Array<any>) => any; |
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.
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.
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
.
[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.
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?
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.
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
NonFunctionPropertyNames
andFunctionPropertyNames
are just the same asPropertyKeysOf
andMethodKeysOf
. 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
,FunctionLike
instead ofConstructable
,MockableFunction
.Finally I used TS builtins
Parameters
andAwaited
instead of customParametersOf
andUnpromisify
. (At the momentConstructorParameters
builtin does not work, but I will figure it out later.)Test plan
Type tests are added.