Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class inherits from tree interface #8

Open
Naskalin opened this issue Dec 3, 2022 · 0 comments
Open

Class inherits from tree interface #8

Naskalin opened this issue Dec 3, 2022 · 0 comments

Comments

@Naskalin
Copy link

Naskalin commented Dec 3, 2022

Good afternoon, first of all thanks for the library. I have a question about converting Tree like structures to Typescript.

  • Question - yes
  • Bug - yes? maybe
  • code for playback
public interface ITree<T>
{
    public ulong Id { get; set; }
    public IEnumerable<T> Children { get; set; }
    public T? Parent { get; set; }
    public ulong? ParentId { get; set; }
}

public class Node : ITree<Node>
{
    public ulong Id { get; set; }
    public IEnumerable<Node> Children { get; set; } = new List<Node>();
    public Node? Parent { get; set; }
    public ulong? ParentId { get; set; }
    public string Title { get; set; } = null!;
}

var typeScript = CsToTs.Generator.GenerateTypeScript(typeof(Node));

expect result

export interface ITree<T> {
    id: number;
    children: Array<T>;
    parent: T;
    parentId?: number;
}

export interface Node extends ITree<Node> {
    id: number;
    children: Array<Node>;
    parent: Node;
    parentId?: number;
    title: string;
}

but got circular error

 at System.Linq.Enumerable.ToList[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Collections.Generic.IEnumerable`1<System.__Canon>)
   at CsToTs.TypeScript.Helper.GetInterfaces(System.Type, CsToTs.TypeScript.TypeScriptContext)
   at CsToTs.TypeScript.Helper.PopulateTypeDefinition(System.Type, CsToTs.TypeScript.TypeScriptContext)
   at CsToTs.TypeScript.Helper+<>c__DisplayClass7_0.<PopulateTypeDefinition>b__0(System.Type)
   at System.Collections.Generic.List`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].ForEach(System.Action`1<System.__Canon>)
   at CsToTs.TypeScript.Helper.PopulateTypeDefinition(System.Type, CsToTs.TypeScript.TypeScriptContext)
   at CsToTs.TypeScript.Helper+<>c__DisplayClass8_0.<GetInterfaces>b__1(System.Type)
   at System.Linq.Enumerable+WhereSelectEnumerableIterator`2[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Cultu
re=neutral, PublicKeyToken=7cec85d7bea7798e]].ToList()

I find workaround

// Skip ITree interface, when converting node class
var text = Generator.GenerateTypeScript(new TypeScriptOptions()
{
    SkipTypePatterns = new [] {"ITree"},
}, typeof(Node));

// Convert separately tree interface
var skippedText = Generator.GenerateTypeScript(typeof(ITree<>));

// Contatenate text + skippedText and Save

With this use, we lose inheritance from the interface, but I did not find another workaround. I would appreciate any help, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant