How to reuse existing C# class definitions in TypeScript projects

Asked
Active3 hr before
Viewed126 times

7 Answers

90%

TypeScript Definition Generator:,https://developercommunity.visualstudio.com/idea/1153873/reuse-existing-net-classes-for-typescript-definiti.html,You can use the open-source project NSwag: In the GUI, you can select .NET class from an existing .NET DLL and generate the TypeScript interface for it. , 5 You now have to install 'TypeScript Definition Generator' manually to use this - see here – Taran Aug 23 '17 at 6:14

There is not currently anything that will map C# to TypeScript. If you have a lot of POCOs or you think they might change often, you could create a converter - something simple along the lines of...

public class MyPoco {
   public string Name {
      get;
      set;
   }
}

To

export class MyPoco {
   public Name: string;
}
load more v
88%

TypeScript Definition Generator:,I am just going to start use TypeScript in my HTML client project which belongs to a MVC project with a entity framework domain model already there. I want my two projects (client side and server side) totally separated as two teams will work on this... JSON and REST is used to communicate objects back and forth.,You can use the open-source project NSwag: In the GUI, you can select .NET class from an existing .NET DLL and generate the TypeScript interface for it. ,https://developercommunity.visualstudio.com/idea/1153873/reuse-existing-net-classes-for-typescript-definiti.html

There is not currently anything that will map C# to TypeScript. If you have a lot of POCOs or you think they might change often, you could create a converter - something simple along the lines of...

public class MyPoco {
   public string Name {
      get;
      set;
   }
}

To

export class MyPoco {
   public Name: string;
}
load more v
72%

I am just going to start use TypeScript in my HTML client project which belongs to a MVC project with a entity framework domain model already there. I want my two projects (client side and server side) totally separated as two teams will work on this... JSON and REST is used to communicate objects back and forth.,There may be a way to optimize/collapse this typing a little bit, but I've got a function prototype for getProperties that I believe achieves what you are looking for.,performaceLog is supposed to work with prototype methods only because it relies on descriptor, which should be optional.,Of course, my domain objects on the client side should match the objects on the server side. In the past, I have normally done this manually. Is there a way to reuse my C# class definitions (specially of the POJO classes in my domain model) to create the corresponding classes in TypeScript"?

There is not currently anything that will map C# to TypeScript. If you have a lot of POCOs or you think they might change often, you could create a converter - something simple along the lines of...

public class MyPoco {
   public string Name {
      get;
      set;
   }
}

To

export class MyPoco {
   public Name: string;
}
load more v
65%

Similarly, namespaces can be used to extend enums with static members:,The declaration merge of Animals in this example:,The visibility rules for merged members is the same as described in the Merging Namespaces section, so we must export the AlbumLabel class for the merged class to see it. The end result is a class managed inside of another class. You can also use namespaces to add more static members to an existing class.,The resulting merged declaration of Document will be the following:

The simplest, and perhaps most common, type of declaration merging is interface merging. At the most basic level, the merge mechanically joins the members of both declarations into a single interface with the same name.

tsinterface Box {
   height: number;width: number;
}
interface Box {
   scale: number;
}
let box: Box = {
   height: 5,
   width: 6,
   scale: 10
};
load more v
75%

One day I was working on a .NET project with Entity Framework. It had a model diagram in a .edmx file, and if I changed this file, I had to select an option to generate the classes for POCO entities (Plain Old CLR Objects).,I used it to generate interfaces and enums for data structures only, but think about the code below:,This code generation was done by T4, a template engine of Visual Studio that worked with a .tt file as a template for a C# class. It ran the code that reads the .edmx model file and outputs the classes in .cs files. After remembering that, I thought that it could be a solution to generate TypeScript interfaces and I started trying to make it work.,It’s much less of a pain to keep this code in sync with all the possible MVC controllers and actions than it was to keep the data structures in sync. But do I need to write this code by hand? Couldn’t it be generated too?

Now with syntax highlighting it was all good. It was time to start writing some code. I searched on google for a working example. My idea was to change it for my needs after I got it working, but… IT DIDN’T WORK!

System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
or one of its dependencies.The system cannot find the file specified.
40%

The following is an example of a TypeScript abstract class definition:,Here is an example as shown in the TypeScript official documentation about interfaces:,Here is an example as shown in the TypeScript official documentation about modules:,Here is an example as shown in the TypeScript official documentation about generic types:

  const myName: string = 'Robert';
load more v
22%

Hey CSS-Tricksters! Bryan Hughes was kind enough to take a concept from an existing post he published on converting to TypeScript and take it a few couple steps further in this post to elaborate on creating reusable base classes. While this post doesn’t require reading the other one, it’s certainly worth checking it out because it covers the difficult task of rewriting a codebase and writing unit tests to help the process.,While working through the best way to design for both TypeScript and vanilla JavaScript, I determined that the best way to solve discrepancies is:,Frontend Masters has a complete learning path for TypeScript from Mike North, one of LinkedIn's deepest TypeScript experts, who will teach you from early fundamentals to production-grade TypeScript.,For those who many not be familiar with using class-based inheritance to enable code sharing, let’s do a quick walk-through of what I mean when I say “creating a base class to reuse code.”

A base class provides structure and common code that other classes can extend. Let’s take a look at a simplified TypeScript example base class that we will extend later:

abstract class Automobile {
   private _speed: number = 0;
   private _direction: number = 0;
   public drive(speed: number): void {
      this._speed = speed;
   }

   public turn(direction: number): void {
      if (direction > 90 || direction < -90) {
         throw new Error(`Invalid direction "${direction}"`);
      }
      this._direction = direction;
   }

   public abstract getNumDoors(): number;
}
load more v

Other "undefined-undefined" queries related to "How to reuse existing C# class definitions in TypeScript projects"