Giter Site home page Giter Site logo

adrianwilczynski / csharptotypescript Goto Github PK

View Code? Open in Web Editor NEW
109.0 109.0 32.0 40.73 MB

Convert C# Models, ViewModels and DTOs into their TypeScript equivalents using webapps, CLI Tool or VSCode extension. Links: https://marketplace.visualstudio.com/items?itemName=adrianwilczynski.csharp-to-typescript - https://csharptotypescript.azurewebsites.net - https://adrianwilczynski.github.io/CSharpToTypeScript/ - https://www.nuget.org/packages/CSharpToTypeScript.CLITool/

License: MIT License

C# 65.67% HTML 11.01% TypeScript 20.25% CSS 0.27% Batchfile 0.84% JavaScript 1.58% SCSS 0.38%

csharptotypescript's People

Contributors

adrianwilczynski avatar dependabot[bot] avatar modesto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

csharptotypescript's Issues

Option to add suffix for the interface names

Would it make sense to add an option --interface-name-suffix <string> to allow modification of the interface's name?

Example: Suppose we have a C# class called ExampleClass. I would like to generate an interface with the name ExampleClassInterface. This could be achieved with the option --interface-name-suffix Interface.

Use case: Later I can easily create a TypeScript class ExampleClass that implements ExampleClassInterface. This I could not do, if the interface itself is already called ExampleClass, or I would need to do something like this:

import {ExampleClass as ExampleClassInterface} from "bla";

It could also be the option --interface-name-prefix I to generate IExampleClass.

Option for no semicolons?

Would you consider adding in a config option to have no semicolons on the output typescript?

Thanks, Carl

TSX support?

Hello,

Is there any plan to support TSX (.tsx) files? TSX is very commonly used in react environments.

Right now the extension is not running for that filetype.

Some Chinese characters trigger error

One example to reproduce the issue is the following:

public class FundWithdrawModel  {
	[DisplayName("手續費")]
	public decimal Fee { get; set; }
}

The character "費" here somehow leads to an error converting. Removing it solves the problem.

Preserve comments

It'd be great to preserve comments which we typically add near field declaration. This way typescript files will have the same documentation.

Does not work when "required" keyword is used in property declarations in class

Example:

    public class ExchangeOverflowCompletedResponse
    {
        public required long OverflowId { get; set; }
        public required Resource Resource { get; set; }
        public required double Quantity { get; set; }
        public required double Fee { get; set; }
    }

Converts to:

export interface ExchangeOverflowCompletedResponse {
    resource: required;
}

Const strings are not converted

We have C# files that define what looks like dictionaries?

That is, it has code like this:

public class Foo {
  public const string AConstant = "A string constant";
  public const string AnotherConstant = "Another string constant";
}

This was converted by the plugin to a structure like this:

export interface Foo {
  aConstant: string;
  anotherConstant: string;
}

So, for one, I'm not sure how it is useful to convert the casing. In my case, I'm converting Razor templates into Vue ones, and they have bindings into these constants, so it's a matter of converting '@Foo.AnotherConstant' to Foo.AnotherConstant, but the casing conversion would make that impossible.

Secondly, of course, the values are missing. I don't understand this kind of conversion because TypeScript has classes, and classes can be used as interfaces, so why isn't the output just:

export class Foo {
  static AConstant = "A string constant";
  static AnotherConstant = "Another string constant";
}

That would be a direct representation of the class.

Types not converted correctly

C#

namespace Business.Models
{
    using System;

    public partial class DealerLender
    {
        public long DealerLenderID { get; set; }
        public string ProgramCode { get; set; }
        public string DealerCode { get; set; }
        public string Name { get; set; }
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string City { get; set; }
        public string StateAbbreviation { get; set; }
        public string ZipCode { get; set; }
        public string PhoneNumber { get; set; }
        public System.DateTime CreateDate { get; set; }
        public System.DateTime UpdateDate { get; set; }
        public Nullable<bool> IsGenericLender { get; set; }
        public Nullable<bool> IsMBPFinanceLender { get; set; }
    }
}

converts to

interface DealerLender {
    dealerLenderID: number;
    programCode: string;
    dealerCode: string;
    name: string;
    address1: string;
    address2: string;
    city: string;
    stateAbbreviation: string;
    zipCode: string;
    phoneNumber: string;
    createDate: string;
    updateDate: string;
    isGenericLender: Nullable<boolean>;
    isMBPFinanceLender: Nullable<boolean>;
}

Anyway we can convert System.DateTime to a Date?
Also nulls should be converted to one of the following:

isGenericLender?: boolean;
isGenericLender: boolean | null;

I'm willing to help as I just found this and we are trying to keep c# and typescript models in sync during our ci/cd pipeline or build process.

Using variable name as TypeScript comment

I don't know if that would be a complicated feature to add (I'll be glad to help but I know nothing about Extension's programming).

My idea would be to add Variables name as a comment name in TypeScript (so it can be viewed with Intellisense), and it could be toggled from extension's preference.

Simple Example :

Indicator.cs

    public class Indicator
    {
        [JsonProperty("n")]
        public string Name { get; set; }
    }

Indicator.ts

export interface Indicator {
    /** Name */
    n: string;
}

String enums for the command line interface

Hey Adrian,

Great package.

Any chance we could have a string Enum option for the command line? I tried just to set it with stringEnums: true but it did not seem to work.

Thanks!

summary tag support

Our c# classes usually add summary tag recognition, and additional tag recognition will make our classes more readable

Fixed string to constant

First of all, thanks for the work put in here 🎉

Just one nuance I came to notice when I wanted to use it in my project: In my codebase in C# I have two types (tracks and albums) which both can be in a list (as IDocuments). Both have the property type on which they both have a fixed name.

By this I can have a union-type in typescript but am able to determine by the property type which of them I have at hand, and the typescript compiler will narrow down the type. This is not possible if I want to use this repo to convert my C# classes to typescript, because it translates the property type to string.

Input

public interface IDocument : IDocument<int>
{ }

public interface IDocument<T>
{
    T Id { get; set; }

    string Type { get; }
}

public class Album : IDocument
{
    public static string TypeName = "album";

    public int Id { get; set; }

    public IEnumerable<Track> Tracks { get; set; }

    public string Type => TypeName;
}

public class Track : IDocument
{
    public static string TypeName = "track";

    public int Id { get; set; }

    public int Length { get; set; }

   public string Type => TypeName;
}

Current output

export interface Document extends Document<number> {

}

export interface Document<T> {
    id: T;
    type: string;
}

export interface Album {
    id: number;
    tracks: Track[];
    type: string;
}

export interface Track {
    id: number;
    length: number;
    type: string;
}

Expected output

export interface Document extends Document<number> {

}

export interface Document<T> {
    id: T;
    type: string;
}

export interface Album {
    id: number;
    tracks: Track[];
    type: "album";
}

export interface Track {
    id: number;
    length: number;
    type: "track";
}

By the expected output, this typescript code would be valid:

(foo: Album | Track) => {
    if (foo.type === "track") {
        foo.length
    }
};

CLI tool improperly recognizing directories

When attempting to use command line tool on a directory with a . in the final folder level, the input is incorrectly assumed to be a file. For instance, C:/A.Directory is assumed to be a file, even if it's a folder name.

The root cause is a result of using a regex to determine filenames at:

public static bool EndsWithFileExtension(this string text)
=> Regex.IsMatch(text, @"\.\w+$");

I'd propose ditching the use of that extension method and just checking both file/directory existence at the same time.

So change the existing:

else if (command.Input.EndsWithFileExtension() && !File.Exists(command.Input))
{
return new ValidationResult($"The file path '{command.Input}' does not exist.");
}
else if (!command.Input.EndsWithFileExtension() && !Directory.Exists(command.Input))
{
return new ValidationResult($"The directory path '{command.Input}' does not exist.");
}

to simply:

else if (!File.Exists(command.Input) && !Directory.Exists(command.Input))
{
    return new ValidationResult($"The file or directory path '{command.Input}' does not exist.");
}

Might resolve #44?

Uri not converted correctly

C#

public Uri Url { get; set; }

converts to

url: Uri;

but should converts to

url: string;

I have dared to do a pull request but I am not sure I have done everything right.

Nullable for non-primitive fields

Since non-primitives in C# are nullable, would it make sense if all the transformed non-primitive fields in the TypeScript interfaces also get nullable?

Example: Suppose we have a C# class ExampleClass with one field a of type NestedClass. Then the transformed TypeScript interface could automatically be:

export interface ExampleClass {
a: NestedClass | null;
}

instead of

export interface ExampleClass {
a: NestedClass;
}

Support dotnet 5

Hi,
I'm trying to install CLI Tool for net5.0 project, but I'm getting compatibility error.

Restoring packages for .NETCoreApp,Version=v5.0...
Resolving conflicts for net5.0...
Checking compatibility of packages on net5.0.
Checking compatibility for shared-enum 1.0.0 with net5.0.
Checking compatibility for CSharpToTypeScript.CLITool 2.5.1 with net5.0.
Package CSharpToTypeScript.CLITool 2.5.1 is not compatible with net5.0 (.NETCoreApp,Version=v5.0). Package CSharpToTypeScript.CLITool 2.5.1 supports: netcoreapp2.2 (.NETCoreApp,Version=v2.2) / any
Invalid project-package combination for CSharpToTypeScript.CLITool 2.5.1. DotnetToolReference project style can only contain references of the DotnetTool type 
Incompatible packages: 2
Package 'CSharpToTypeScript.CLITool 2.5.1' has a package type 'DotnetTool' that is not supported by project 'shared-enum'.

Is it possible to add support for net5.0?
I'm working on converting backend enums and interfaces to TypeScript files. We're going to automate it and add it to the build pipeline in order to keep interfaces and enums the same on both sides.

DateOnly not converting to string

public class Sample
{
  public DateTime DT { get; set; }
  public DateOnly DO { get; set; }
}

converts to

export interface Sample {
  dT: string;
  dO: DateOnly;
}

I would expect it DateOnly to convert to a string just like DateTime

Generate one class/interface/enum per file

Hi @AdrianWilczynski ,
the VS Code extension and CLI tools are great, very helpful and already saved me a lot of time 😉 Thanks for your great job!

However, I have some issue and want to ask you first, maybe you know something about that. I see that normally if I have few classes/interfaces/enums in a single C# file, cs2ts generates a single file with all these objects from C# file. However, in some cases from a single .cs file I have 2 .ts files generated - both with the same content (2 interfaces inside).

I'm also using import-generation = Simple. The issue is that sometimes few classes are defined in a single .cs file, but are needed in other .cs files (multiple), so the default naming convention for imports generation doesn't work properly.

What I'd like to achieve is that cs2ts generates a separate .ts file for each class/interface/enum it finds in the source .cs file. Do you know if this is somehow already supported?
If not, where should I look in the source code to contribute and add such a feature?

Thanks a lot for answering 😉

Option to add custom type mapping

I have a C# class with a property of type IFormFile and I would like to have CSharpToTypeScript transform this property to the type of a TypeScript File.

I suggest adding the option to add custom mapping rules (similar to Dates), e.g.

--addCustomMapping IFormFile File

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.