Skip to content
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): simplify PropertyLikeKeys utility type #13020

Merged
merged 2 commits into from Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
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
Expand Up @@ -374,8 +374,6 @@ expectType<SpyInstance<typeof indexSpiedObject.methodE>>(
spyOn(indexSpiedObject, 'methodE'),
);

expectError(spyOn(indexSpiedObject, 'propertyA'));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same test is repeating few lines bellow.


expectType<SpyInstance<() => {a: string}>>(
spyOn(indexSpiedObject, 'propertyA', 'get'),
);
Expand Down
10 changes: 5 additions & 5 deletions packages/jest-mock/__typetests__/utility-types.test.ts
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fells like string | number is more correct or at least more TypeScriptish.

A note from TS docs: keyof {[k: string]: any} is of type string | number, "because JavaScript object keys are always coerced to a string".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checked, TypeScript allows this:

type IndexObject = {
  [key: string]: Record<string, any>;
};

const indexObject: IndexObject = {
  123: {a: 123},

  prop: {b: 'abc'},
};

So it feels like this is correct (or?):

type IndexObject = {
  [key: string]: Record<string, any>;
};

type PropKeys = PropertyLikeKeys<IndexObject>; // `string | number`

expectType<'propertyA' | 'propertyB' | 'someClassInstance'>(objectProperties);
expectType<string>(indexObjectProperties);
expectType<string | number>(indexObjectProperties);
11 changes: 4 additions & 7 deletions packages/jest-mock/src/index.ts
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