-
-
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
fix(jest-mock): tweak typings to allow jest.replaceProperty()
replace methods
#14008
fix(jest-mock): tweak typings to allow jest.replaceProperty()
replace methods
#14008
Conversation
jest.replace()
replace methodsjest.replaceProperty()
replace methods
T extends object, | ||
K extends PropertyLikeKeys<T>, | ||
> = { | ||
type ReplacedPropertyRestorer<T extends object, K extends keyof 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.
I think that this should align with:
function replaceProperty<T, K extends keyof T>(obj: T, key: K, value: T[K]): ReplaceProperty<T[K]>;
in the types packages.
Currently (without this change) getting a typescript error:
node_modules/@jest/environment/build/index.d.ts:401:26 - error TS2430: Interface 'JestImportMeta' incorrectly extends interface 'ImportMeta'.
The types of 'jest.replaceProperty' are incompatible between these types.
Type '<T extends object, K extends Exclude<keyof T, keyof { [K in keyof T as Required<T>[K] extends ClassLike ? K : never]: T[K]; } | keyof { [K in keyof T as Required<T>[K] extends FunctionLike ? K : never]: T[K]; }>, V extends T[K]>(object: T, propertyKey: K, value: V) => Replaced<...>' is not assignable to type '<T, K extends keyof T>(obj: T, key: K, value: T[K]) => ReplaceProperty<T[K]>'.
Types of parameters 'object' and 'obj' are incompatible.
Type 'T' is not assignable to type 'object'.
401 export declare interface JestImportMeta extends ImportMeta {
~~~~~~~~~~~~~~
node_modules/@types/jest/index.d.ts:331:30
331 function replaceProperty<T, K extends keyof T>(obj: T, key: K, value: T[K]): ReplaceProperty<T[K]>;
~
This type parameter might need an `extends object` constraint.
So basically one of the issues is fixed here by not using PropertyLikeKeys
anymore but T extends object
is still incompatible with T
so should likely also become just T
here.
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.
Hm.. I think it would be better to have T extends object
in @types/jest
. It looks right to have a constrain here.
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.
As long as it's aligned I guess. But I don't think object
is a good choice because object
means "all non-primitive" types. So any class instances would also match that, for example. I don't know jest well enough to suggest what it should be but perhaps Record<.., ..>
would be a better fit?
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 all non-primitive types are objects and if objects can have properties, I think it is alright to assume that these objects can have properties and that these properties might get replaced. Sometimes people add extra properties on arrays or functions. For instance, expect()
has expect.not.stringContaining()
which is the property of that function.
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.
Just opened DefinitelyTyped/DefinitelyTyped#64936
Can you merge in main so we can verify the changes on CI? |
Done. All is green (; |
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.
thanks!
@SimenB is there a plan for releasing a 29.5.1 or something with this fix merged? |
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. |
Reference DefinitelyTyped/DefinitelyTyped#64743
Summary
Current typing error, if a method is being replaced using
jest.replaceProperty()
.As it was pointed out in the above mentioned PR,
jest.replaceProperty()
is actually allowed to replace methods, not only properties. Even documentation is mentioning this use case (see last line):Test plan
Type test is added.