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

createHydratedMock() should return Required<T> #1494

Open
hosswald opened this issue Sep 12, 2023 · 1 comment
Open

createHydratedMock() should return Required<T> #1494

hosswald opened this issue Sep 12, 2023 · 1 comment

Comments

@hosswald
Copy link

Subject of the issue

createHydratedMock() mocks optional properties. The typescript compiler should know about that to avoid having to write unnecessary as any in tests.

Your environment

  • ts-auto-mock version: 3.7.1
  • typescript version: 5.1.6
  • node version: 20.5.0
  • npm version: 9.8.1

Expected behavior

    const foo = createHydratedMock<{bar?: string}>() // typeof foo == Required<{bar?: string} == {bar: string}
    const baz: string = foo.bar

Actual behavior

    const foo = createHydratedMock<{bar?: string}>()
    const baz: string = foo.bar // TS2322: Type 'string | undefined' is not assignable to type 'string'.
@uittorio
Copy link
Member

Hi @hosswald

Instead of using any could you try using

createHydratedMock<{bar?: string}>() as Required<{bar?: string}

This should prevent you casting to any.

In response of what it should return createHydratedMock.

Required would be slightly incorrect because it doesn't consider nested properties in the object, just the first level.

The purpose of ts-auto-mock was not changing the original type but mutating values. I understand where you are coming from and it something that we could have discuss further if we were actively maintaining the library!!

Happy to see a PR with a proposal!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants