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

Allow methods to return custom-typed data via use of TypeScript generics #47

Open
mattpetrie opened this issue Aug 20, 2020 · 0 comments

Comments

@mattpetrie
Copy link
Contributor

This is an idea that I had after something that came up recently on Marketplace Mobile.

In MMA, we are using custom TypeScript types to represent custom GraphQL scalar types in TS. Through this method, a DayString, for example, can be used in place of a plain string, but a plain string can't be used as a DayString.

However, when generating fake data for something that is type as one of these custom types, the default returned value from fake-eggs won't satisfy the type (since, for example, fake.day()'s return type is string), which then necessitates type casting the fake data before it can be used.

One way to avoid having to typecast in this situation would be to give the fake.day and similar methods a generic type, so that you could do something like:

const day: DayString = fake.day<DayString>();

to have fake.day return a DayString instead of a plain string.

string could still be the default type so that fake.day() could still be used without having to provide a type in most general cases.

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

No branches or pull requests

1 participant