Skip to content

fix: correct function type for spyon an optional function #19240

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

Merged
merged 7 commits into from
May 27, 2025

Conversation

li-yechao
Copy link
Contributor

@li-yechao li-yechao commented Apr 24, 2025

What does this PR do?

Mock type is incorrect when spyOn a nullable function, like this:

  const test: {
    optionalMethod?: (input: { question: string }) => { answer: string };
  } = {
    optionalMethod: (input) => ({ answer: `Aswer to ${input.question}` }),
  };

  const a = spyOn(test, "optionalMethod");

typeof a is Mock<never>

How did you verify your code works?

@RiskyMH RiskyMH added the types An issue with TypeScript types label Apr 25, 2025
@alii alii self-requested a review April 25, 2025 04:51
alii
alii previously approved these changes Apr 25, 2025
@alii alii merged commit 325d0b1 into oven-sh:main May 27, 2025
1 of 2 checks passed
@alii
Copy link
Member

alii commented May 27, 2025

Thanks for the contribution!

@li-yechao li-yechao deleted the fix/spyon-optional-function branch June 5, 2025 00:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types An issue with TypeScript types
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants