0

I have a react/c#.NET web project where i would like to share models between server and client. I currently have several c# models on the server side similar to following:

public class Model1 {
    public string Property1 {get; set;}
    public string Property2 {get; set;}
}

I am passing these back to client and i am wondering if there is a way that i am unaware of where you can share the models somehow. Currently i am just manually building up a typescript model on client side which is fine but redundant. Following is example of what above class would yield in ts:

export default class Model1 {
    Property1: string;
    Property2: string;
}
Maxqueue
  • 2,194
  • 2
  • 23
  • 55
  • How about [this](https://stackoverflow.com/questions/48693335/how-to-generate-basic-typescript-interfaces-from-swagger-schema)? – Yuriy Faktorovich Dec 29 '21 at 04:32
  • @Yuriy Faktorovich Ya similarly i am using https://csharptotypescript.azurewebsites.net/ but still redundant. Trying to avoid having duplicate models if possible – Maxqueue Dec 29 '21 at 04:37
  • Okay, so I have a crazy idea. You create an endpoint in the .net api which actually returns typescript files based on your entity models. Then you can just use the url of that endpoint in your import. Seems like a lot of work, but it'd be pretty cool if you could pull it off. – Yuriy Faktorovich Dec 29 '21 at 04:48
  • The other crazy idea would be for .net to generate the typescript files on a build, or maybe have a console app to do it, then drop them in your react src. Either way, you'd need the typescript files to somehow be generated by the .net piece. – Yuriy Faktorovich Dec 29 '21 at 04:51
  • @Yuriy Faktorovich Yes something like what you suggested is what i want. Was hoping there was some tool that already does this to save time. Will have to give this some more thought. Importing via url is intriguing. – Maxqueue Dec 29 '21 at 04:56

0 Answers0