fix(jest-mock): improve spyOn typings to handle optional properties#13247
fix(jest-mock): improve spyOn typings to handle optional properties#13247SimenB merged 5 commits intojestjs:mainfrom mrazauskas:fix-spyOn-optional-props
spyOn typings to handle optional properties#13247Conversation
| spyOn< | ||
| T extends object, | ||
| K extends ConstructorLikeKeys<Required<T>>, | ||
| V extends Required<T>[K], |
There was a problem hiding this comment.
V is just a helper to avoid SpyInstance<(...args: ConstructorParameters<Required<T>[K]>) => InstanceType<Required<T>[K]>>
| spyOn<T extends object, K extends PropertyLikeKeys<T>>( | ||
| object: T, | ||
| methodName: M, | ||
| methodKey: K, |
There was a problem hiding this comment.
methodKey and K are just renames for consistency. MethodLikeKeys, PropertyLikeKeys, methodKey, propertyKey, K – always keys. Easier to understand what is happening.
| spyOn< | ||
| T extends object, | ||
| K extends MethodLikeKeys<Required<T>>, | ||
| V extends Required<T>[K], |
There was a problem hiding this comment.
Lines with Required do all the magic, because they turn types like (() => void)) | undefined into () => void). Something what wasn’t a function, becomes one. This part was not working, otherwise logic is the same.
| private _spyOnProperty<T extends object, K extends PropertyLikeKeys<T>>( | ||
| obj: T, | ||
| propertyName: M, | ||
| accessType: 'get' | 'set' = 'get', |
There was a problem hiding this comment.
Default is unnecessary, accessType is always passed:
|
Interestingly same things here DefinitelyTyped/DefinitelyTyped#62208 fails to compile on TS 4.4. But in Jest repo all is fine on TS 4.3. I always have a feeling that to go through CI on DT is simply luck ;D |
|
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
Apparently
spyOnoverloads do not work properly if an interfaces with optional props is encountered:So this got fixed.
Test plan
Type tests are added.