Skip to content

Commit

Permalink
refactor(jest-mock): simplify PropertyLikeKeys utility type (#13020)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas authored Jul 13, 2022
1 parent 3d94b58 commit b3d4a1d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

- `[jest-changed-files]` Fix a lock-up after repeated invocations ([#12757](https://github.com/facebook/jest/issues/12757))
- `[@jest/expect-utils]` Fix deep equality of ImmutableJS OrderedSets ([#12977](https://github.com/facebook/jest/pull/12977))
- `[jest-mock]` Add index signature support for `spyOn` types ([#13013](https://github.com/facebook/jest/pull/13013))
- `[jest-mock]` Add index signature support for `spyOn` types ([#13013](https://github.com/facebook/jest/pull/13013), [#13020](https://github.com/facebook/jest/pull/13020))
- `[jest-snapshot]` Fix indentation of awaited inline snapshots ([#12986](https://github.com/facebook/jest/pull/12986))

### Chore & Maintenance
Expand Down
2 changes: 0 additions & 2 deletions packages/jest-mock/__typetests__/mock-functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,6 @@ expectType<SpyInstance<typeof indexSpiedObject.methodE>>(
spyOn(indexSpiedObject, 'methodE'),
);

expectError(spyOn(indexSpiedObject, 'propertyA'));

expectType<SpyInstance<() => {a: string}>>(
spyOn(indexSpiedObject, 'propertyA', 'get'),
);
Expand Down
10 changes: 5 additions & 5 deletions packages/jest-mock/__typetests__/utility-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ type IndexObject = {

methodA(): void;
methodB(b: string): boolean;
methodC: (c: number) => true;
methodC: (c: number) => boolean;

propertyA: {a: 123};
propertyB: {b: 'value'};
propertyA: {a: number};
propertyB: {b: string};
};

// ClassLike
Expand Down Expand Up @@ -142,6 +142,6 @@ declare const objectProperties: PropertyLikeKeys<SomeObject>;
declare const indexObjectProperties: PropertyLikeKeys<IndexObject>;

expectType<'propertyA' | 'propertyB' | 'propertyC'>(classProperties);
expectType<string>(indexClassProperties);
expectType<string | number>(indexClassProperties);
expectType<'propertyA' | 'propertyB' | 'someClassInstance'>(objectProperties);
expectType<string>(indexObjectProperties);
expectType<string | number>(indexObjectProperties);
11 changes: 4 additions & 7 deletions packages/jest-mock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@ export type MethodLikeKeys<T> = keyof {
[K in keyof T as T[K] extends FunctionLike ? K : never]: T[K];
};

export type PropertyLikeKeys<T> = {
[K in keyof T]: T[K] extends FunctionLike
? never
: T[K] extends ClassLike
? never
: K;
}[keyof T];
export type PropertyLikeKeys<T> = Exclude<
keyof T,
ConstructorLikeKeys<T> | MethodLikeKeys<T>
>;

// TODO Figure out how to replace this with TS ConstructorParameters utility type
// https://www.typescriptlang.org/docs/handbook/utility-types.html#constructorparameterstype
Expand Down

0 comments on commit b3d4a1d

Please sign in to comment.