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

Generics in the return type #81

Closed
countnazgul opened this issue Sep 16, 2022 · 0 comments
Closed

Generics in the return type #81

countnazgul opened this issue Sep 16, 2022 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@countnazgul
Copy link
Member

At the moment the return type of all methods is IHttpReturn. It is defined as:

interface IHttpReturn {
  status: number;
  statusText: string;
  message?: string;
  data: AxiosResponse["data"];
}

With such declaration the data property will be of type any:

data-any-type

Although this is not an issue itself it's not a good practice to return any.

Another inconvenience is that when using the method

  • the data have to be cast to the expected type
    image
  • or even left without a type. And this will be a valid syntax since the return type is any

And because of these the code, that uses the method, can potentially throw an runtime error, which could have been avoided if the method was requiring type

The solution is to allow the methods return type to accept generics. If return type is not provided when calling the method then its data property will be of type unknown and any attempt to use some of the data's properties will result an error in the editor:

error-unknonw

But if return type is provided then the return data properties are "known" and can be used:

no-error-generics

The provided return type will be applied only to the data property of the IHttpReturn interface. The rest of the properties will behave the same (even without specified return type):

other-properies

The new IHttpReturn definition will be something like:

new-interface-definition

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

No branches or pull requests

1 participant