question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Partial C# classes result in multiple conflicting TS classes

See original GitHub issue

This is fairly simple to replicate:

Dummy.a.cs public partial class Dummy { public string A {get; set;} ... }

Dummy.b.cs public partial class Dummy { public int B {get; set;} ... }

dummy.tst

$Classes(Dummy)[
    export class $Name {$Properties[
        public $name: $Type;]
    }]

Output:

Dummy.a.ts

export class Dummy { 
    public a: string;  
    public b: number;
}

Dummy.b.ts

export class Dummy { 
    public a: string;  
    public b: number;
}

This doesn’t compile in TypeScript due to the duplicates. It should either output a single Dummy.ts file or include something in the template for later files to extend (rather than re-define) the class.

The $Classes method should iterate each class, not each file that holds part of a class.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
frhagncommented, Dec 28, 2016

Hi guys, I’ve just released a new version with improved support for partial types (1.10.0). The solution is very similar to @KeithHenry’s suggestion using different modes that can be configured in the template constructor.

${
    Template(Settings settings)
    {
        settings.PartialRenderingMode = PartialRenderingMode.Combined;
    }
}
...

PartialRenderingMode.Partial: Partial types are rendered as defined in the c# source containing only the parts defined in each file. This is the default rendering mode.

PartialRenderingMode.Combined: Partial type definitions are combined to a single type acting as if the full type was defined only once in the c# source (using the filename of the first file containing a part of the type).

PartialRenderingMode.Legacy: A combined type definition are rendered for each file containing a partial definition.

Please try it out and let me know if it solves your issues 😃

0reactions
petrosmmcommented, Mar 26, 2020

Hello all,

I absolutely love the typewriter concept, however, some of my files (one for example) have public partial class ApplicationUser {} twice in the same file reporting a duplicate class declaration. I cannnot seem to figure out what I am doing wrong. The template I am using is below.

The output reports as

module App {
    export class ApplicationUser { 
      // all props
    }

    export class ApplicationUser { 
      // all props
    }
}
${
    public static readonly char[] ending = ".cs".ToCharArray();

	     static string debugInfo = "";
			string PrintDebugInfo(File f){
			debugInfo = string.Join(", ", f.Classes.GroupBy(p => p.Name).Select(p => p.FirstOrDefault()).Select(p => p.Name).ToArray());
          return debugInfo;        
     }

    Template(Settings settings)
    {   
			settings.PartialRenderingMode = PartialRenderingMode.Combined;
        settings.IncludeProject("MyProjectNamespace.Entities");

        settings.OutputFilenameFactory = file =>        {            
			return $"{file.Name.TrimEnd(ending).ToLower()}.gen.ts";       
		};
    }

	 IEnumerable<Class> Imports(File f) {
		var classes = f.Classes;
        return classes.GroupBy(p => p.Name).Select(p => p.FirstOrDefault()).ToArray();
     }
}

module App {$Classes(*)[
    export class $Name { $Properties[
        public $name: $Type;]
    }	
	]
}

//   $PrintDebugInfo
Read more comments on GitHub >

github_iconTop Results From Across the Web

C# Partial Classes
In short, you can't use partial classes across projects. All the source must be compiled at the same time, and that's done per...
Read more >
How to extend an existing TypeScript class without a ...
It makes extending an existing class very simple: just declare the class as partial and extend it freely. It is especially useful when...
Read more >
TypeScript: Documentation - Modules
Classes and function declarations can be authored directly as default exports. Default export class and function declaration names are optional.
Read more >
Practical Ways to Write Better JavaScript
When TS is setup correctly, it will be difficult to write code without first defining your interfaces and classes. This provides a way...
Read more >
Content Configuration
Configuring source paths. Tailwind CSS works by scanning all of your HTML, JavaScript components, and any other template files for class names, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found