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

[Bug]: spyOn type definition doesn't work with index signatures #12966

Closed
Maxim-Mazurok opened this issue Jun 26, 2022 · 2 comments
Closed

[Bug]: spyOn type definition doesn't work with index signatures #12966

Maxim-Mazurok opened this issue Jun 26, 2022 · 2 comments

Comments

@Maxim-Mazurok
Copy link
Contributor

Version

28.1.1

Steps to reproduce

  1. Add this into your test file:
type DoesNotWork = {
  [index: string]: {
    [x: string]: any;
  };

  methodOne: () => void;
  methodTwo: () => void;
};

const doesNotWork = {} as DoesNotWork;

jest.spyOn(doesNotWork, "methodOne"); // error: No overload matches this call
  1. Observe an error: No overload matches this call

Expected behavior

I expect it to work as well as in this case:

type Works = {
  methodOne: () => void;
  methodTwo: () => void;
};

const works = {} as Works;

jest.spyOn(works, "methodOne"); // no error

Actual behavior

error: No overload matches this call

Additional context

The problem is in FunctionPropertyNames utility type, it doesn't work as expected in this case.

type Testing = {
  [index: string]: {
    [x: string]: any;
  };

  methodOne: () => void;
  methodTwo: () => void;
};

type FunctionProperties<T> = {
  [K in keyof T]: T[K] extends (...args: any[]) => any ? K : never;
};

/**
 * {
 *   [x: string]: never;
 *   methodOne: "methodOne";
 *   methodTwo: "methodTwo";
 * }
 */
type functionProperties = FunctionProperties<Testing>;

// never
type values = functionProperties[keyof functionProperties];

My real-life use-case is to do this:

import { google } from "googleapis";
jest.spyOn(google, "sheets");

getting

No overload matches this call.
  Overload 1 of 3, '(object: GoogleApis, method: never): never', gave the following error.
    Argument of type 'string' is not assignable to parameter of type 'never'.
  Overload 2 of 3, '(object: GoogleApis, method: never): never', gave the following error.
    Argument of type 'string' is not assignable to parameter of type 'never'.ts(2769)

Environment

System:
    OS: Windows 10 10.0.22000
    CPU: (12) x64 AMD Ryzen 5 3600 6-Core Processor
  Binaries:
    Node: 17.2.0 - ~\AppData\Local\nvs\node\17.2.0\x64\node.EXE
    npm: 8.1.4 - ~\AppData\Local\nvs\node\17.2.0\x64\npm.CMD
  npmPackages:
    jest: ^28.1.1 => 28.1.1
@Maxim-Mazurok
Copy link
Contributor Author

Fixed by DefinitelyTyped/DefinitelyTyped#60971

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant