Start a new topic
Answered

Using duocode generated javascript from typescript

If I try to use the generated javascript from typescript I'm having some troubles understanding what is going on.

Lets say I have A c# code like this:

 

using System;

namespace Company.Product.Presentation.Model
{
    //base class
    public class UserInterface
    {
        protected readonly IApplicationService _applicationService;

        public UserInterface( IApplicationService appService) 
        {
            _applicationService= appService;
        }

        //value object
        public Data SomeData{ get; set; }
//property
        public string UserId { get; set; }
//method
        public virtual void Execute()
        {
            //
            _applicationService.Execute();
        }
    }

    public class WebUserInterface:UserInterface
    {
        public WebUserInterface(IApplicationService appService):
            base(appService) 
        {

        }
        public override void Execute()
        {
            //dosomething here
            _applicationService.ExecuteSpecial();
        }
    }

  public struct Data
{
public int prop1{get;set;}
public int prop2{get;set;}
}
}

Then I define a typescript definition file(I'm doing it for the first time so it might be wrong): 

declare module Company{
    module Product{
        module Presentation {
            module Model {
                class UserInterface {
                     UserId: string;
                    Data: CreditMeters;
                    constructor(api: Company.Product.Common.IApplicationService);
                    Execute();
                }

                class WebUserInterface extends UserInterface {
                    constructor(api: Company.Product.Common.IApplicationService);
                    Execute();
                }

                class Data{
                    prop1: number;
                    prop2: number;
                }
            }
        }
    }
}

and try to use it in "app.ts" like this....


 

window.onload = () => {

    var api = ...

    //this one doesn't seem to call the generated js constructor?!
    var pmod = new Company.Product.Presentation.Model.WebUserInterface(api);
    //now, there are no Data neither Execute on this pmod?! 
    pmod.Execute();//error 0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'Execute'?
    pmod.Data;//error 
};

 What am I doing wrong here? Why pmod does not have Execute and Data?  

 



Best Answer

In order to create instance of C# type in JS context you should use (.ctor), this way:

 

new Company.Product.Presentation.Model.WebUserInterface.ctor(api);


Since in C# you can have ctors overloaded you must use .ctor, .ctor$1, .ctor$2 and etc.


In next version, we are going to throw exception:

   

Error: can't construct instance this way, use 'full_type_name.ctor()' instead

   



Answer

In order to create instance of C# type in JS context you should use (.ctor), this way:

 

new Company.Product.Presentation.Model.WebUserInterface.ctor(api);


Since in C# you can have ctors overloaded you must use .ctor, .ctor$1, .ctor$2 and etc.


In next version, we are going to throw exception:

   

Error: can't construct instance this way, use 'full_type_name.ctor()' instead

   


Ok, it worked for the given example but it choked on a class implementing an interface.

This code:

 

    public interface ITest 
    {
        void DoSomething();
    }

    public class Test : ITest 
    {
        public void DoSomething() 
        {
            Console.WriteLine("DoSomething");
        }
    }

when called with a t.ds declaration... 

declare module TT{            
    export interface ITest {
                DoSomething();
            }
            export class Test implements ITest {
                static ctor():Test;
                DoSomething();
            }
}

 and I do:

var test = TT.Test.ctor();

it won't go through this code in mscorlib.js:

System.Object = $d.declare("System.Object", Object, 1, $asm, function($t, $p) {
    $t.$typeInfo = function(t, p) { return [1, null, [["GetType", p.GetType, 6], ["ToString", p.ToString, 6], ["toString", p.toString, 3], ["Equals", p.Equals, 6], ["Equals", t.Equals, 22], ["GetHashCode", p.GetHashCode, 6], ["ReferenceEquals", t.ReferenceEquals, 22]], [["ctor", t.ctor, 6]]]; };
    $t.ctor = function Object() {
        var type = this.constructor;

        if (type.cctor)
            type();

        while (type != $t) {
            if (type.$ator)
                type.$ator.call(this);
            type = type.$baseType;
        }
    };

 $baseType is undefined when implementing an interface?

Hm, it is not working neither for the example. Same issue with type= type.$baseType resulting in undefined.

I've made it work changing the typescript definition like this:

    export interface ITest {
        DoSomething();
    }
    export class Test implements ITest {
        static ctor():void;
        DoSomething();
    }

 now I have to call like :

var test = new Test.ctor();

Thanks Igal!


Wait for our next official release of DuoCode, with this version you will be able to auto-generate .d.ts files for C#, and even combine TypeScript & C# code in the same project (no need for separate projects).


Stay tuned.


Do you have any list of upcoming features and roadmaps for DuoCode?

Have you tried our latest DuoCode v1.0 with TypeScript support, and even hybrid C# + TypeScpirt projects.

Download it from: http://duoco.de/download

I did just a quick preview of the generated d.ts. It helped me solving the delegate issue. Will look at the hybrid feature soon. It is surely a great value added for ts/js projects using duocode generated javascript as a library.

I still use it "inversed" since caling js/ts libs out from the c#(DuoCode) is not yet optimal. So I call DuoCode generated js from ts/js. Binding might change that. 

Robert, one more clarification on this issue.

The upcoming version 1.1 improves .d.ts generation with strongly typed delegates, generics, ref/out params and more. As well as better hybrid C# + TS projects support.

Stay tuned, v1.1 will be released by the end of this month.


Login or Signup to post a comment