-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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
Interface vs DTO #1228
Comments
From the nestjs docs :
|
I read that, but I am not sure what it means. Is Nestjs suggesting the usage of DTO instead of interface in every case ? In the other hand, I can find in the doc exemples mixing interfaces and dtos |
@rlataguerra DTOs and interfaces are not two distinct things. DTOs can be implemented as either interfaces or classes, and the latter is clearly recommended by the documentation. Either way, you create DTOs that define the shape of data being sent or received - it's just a matter of preference how you actually define those. |
Not only preference, but also use case. Interfaces can not compute properties or use decorators, whereas classes can. NestJS recommends to use classes because you can add decorators from |
@BrunnerLivio has explained the main difference between DTOs and interfaces very well. Actually, this is the primary reason why classes are recommended in the ofc docs |
thanks for clarifications 👍 |
@rlataguerra Thanks for posting the questions! Going through the documentation is confusing as the examples use both, even the github repo has the same. Can we improve documentation for new users to easily understand? Can we just use class throughout examples and at the end provide an alternate mentioning can be used as an interface. |
who didn't understand what is DTO, and why it can be at same time Interface and Class, |
Well that already exits by using
EDIT Cooked up this to remove values form the request that are not in the DTO .. going to look at adding validation as well
|
In our use case the DTO doesn't include properties that aren't already present in the entity, so we created an additional interface with the DTO parameters, which the DTO class as well as our entity class implements. That way we have a 'single point of truth' as to which parameters need to be implemented by both. Maybe it's a bit overkill, but as I understood, typescript interfaces aren't compiled and are here to guide you while programming. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
[ ] Regression
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
I have some troubles to figure it out when to use an interface and when to use a DTO.
Would you mind to add some details about dtos and interfaces in the doc (when to use what... etc)
The text was updated successfully, but these errors were encountered: