I have an other thought on the topic, And I think this is an approach that is promoted/adopted by the golang community(correct me if i am wrong).
What about having only one global (maybe in a types directory) user type/interface and other versions with only partial set of fields, they are only defined local in the module that uses them.
I have this thought as well. Ideally, we should have interfaces corresponding to the data model represented by the database tables, and I remember that there are ORM and Code Generation tools that can do this.
In addition, for the front end, we should define the types and interfaces for the view model.
The model(type, interface) conversion process is as follows:
API => Frontend representation: Domain Model Interface => View model interface
Frontend representation => API: View model interface => Domain Model interface.
We will convert the domain model interface that contain all fields to target types through TS's Utility Types or Map Type rather than repeated definitions.
The naming convention for the derived types just like my above comment. The key point of the naming convention should be readability, scalability, reusability and maintainability.
More naming convention examples:
typeUserEntity{id:string;name:string;role:string;email:string;}// Comsumer side// For react hooktypeUseGetUserQueryPayload={id:Pick<UserEntity,'id'>}constuseGetUserQuery=(payload:UseGetUserQueryPayload)=>{}// For API service methodstypeGetUsersResult=Pick<UserEntity,'id'|'name'>;constgetUsers=():Promise<GetUsersResult>=>fetch(/.../)// But what name should be use for below situationtypeUserOfProduct=Pick<UserEntity,'id'|'name'>;typeProduct={id:string;name:string;// I think it's not prefect. // UserOfProduct is same with GetUsersResult, duplicated!user:UserOfProduct;}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I have an other thought on the topic, And I think this is an approach that is promoted/adopted by the golang community(correct me if i am wrong).
What about having only one global (maybe in a types directory)
usertype/interface and other versions with only partial set of fields, they are only defined local in the module that uses them.I have this thought as well. Ideally, we should have interfaces corresponding to the data model represented by the database tables, and I remember that there are ORM and Code Generation tools that can do this.
In addition, for the front end, we should define the types and interfaces for the view model.
The model(type, interface) conversion process is as follows:
API => Frontend representation: Domain Model Interface => View model interface
Frontend representation => API: View model interface => Domain Model interface.
We will convert the domain model interface that contain all fields to target types through TS's Utility Types or Map Type rather than repeated definitions.
The naming convention for the derived types just like my above comment. The key point of the naming convention should be readability, scalability, reusability and maintainability.
More naming convention examples: