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

Loose inferred return type of generator function #114

Open
purepear opened this issue Feb 16, 2023 · 2 comments
Open

Loose inferred return type of generator function #114

purepear opened this issue Feb 16, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@purepear
Copy link

Description

The inferred type of the generator allows for extra properties(variant 1).
In the case of class extension it also requires narrowing the literal types(variant 3).
It seems to work well when the return type is set explicitly(variant 2, variant 4).

Looking at the source code(the generic types), I would expect the generator return type to be inferred properly 🤔

I'm not sure if this is a bug or a feature 😄

image

To Reproduce

https://stackblitz.com/edit/fishery-test?file=Post.ts

Additional context
Fishery version: 2.2.2

@purepear purepear added the bug Something isn't working label Feb 16, 2023
@pangolingo
Copy link
Contributor

I have observed this behavior too.

This is a Typescript bug/feature (I also am not sure which to call it).
Here's some details:

I don't know if we can fix this without getting it resolved in Typescript itself. We're open to suggestions/PRs if you have an idea!

Usually that extra parameter doesn't cause any trouble. If it does, add the explicit return type from your examples:

Factory.define<Post>(
  (): Post => ({ ... })
)

@pangolingo
Copy link
Contributor

I don't know why Typescript requires you to narrow your type (status: 'published' as const) for the class extension but not the factory. They're both using the same define function internally. Probably because the Factory type definition (Factory<Post>) happens a little bit further away from the function definition. I'm not sure what we could do for a fix there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants