haxiomic / dts2hx

Converts TypeScript definition files (d.ts) to haxe externs (.hx) via the TypeScript compiler API
MIT License
135 stars 9 forks source link

Support shared properties of type unions #105

Open sonygod opened 2 years ago

sonygod commented 2 years ago

ts.AnyOf issue.

some gen class have some fields like

        var geometry : ts.AnyOf2<A,B>;

        geometry .AProperty=1;//current will throw error.

       geometry.BProperty=2;//current will throw error.

//is possible to do something like that?

haxiomic commented 2 years ago

I’m not sure I follow - could you explain a bit more? What does the source typescript look like?

Do you mean those properties are shared between types A and B and should be accessible within the AnyOf?

I have a plan for this but it requires macros, it’s in the roadmap but less critical than other features

sonygod commented 2 years ago
     Do you mean those properties are shared between types A and B and should be accessible within the AnyOf?

yes ,I mean those properties are shared between types A and B,because we have a project use native js ,there are lots of these

style something like

         class  SomeClass {
           var geometry : ts.AnyOf2<A,B>;
     }

        var sc=new SomeClass ();

       sc.geometry .AProperty=1;

       var sc2=new SomeClass ();

      sc.geometry .BProperty=1;

I convert to haxe I have to write lots of cast ugly code ...

   cast (sc.geometry,A) .AProperty=1;         
   cast (sc.geometry,B) .BProperty=1;
haxiomic commented 2 years ago

Agreed, it can be solved with a type building macro which is on the roadmap I'll rename the issue and leave it open

btw this also works

sc.asType0.AProperty = 1;
sc.asType1.BProperty = 1;
sonygod commented 2 years ago

there is more complex in three.js

              class  SomeClass {
           var geometry : ts.AnyOf2<AParent,BParent>;
     }

        var sc=new SomeClass ();

       sc.geometry .AChildClasssProperty=1;//native js  direct support  child's property.

Mesh.hx

 extern class Mesh extends Object3D

var material : ts.AnyOf2<Material, Array<Material>>;

                myobject .material.color.set(iColor);//base Material have no color property 
                  myobject.material.needsUpdate = true;//base Material have no needsUpdate property 
sonygod commented 2 years ago

@haxiomic ,dts2hx 0.18.1 seem support this ? I will close this issue.

three.js 0.135

seem resolution this issue.

    Line<TGeometry, TMaterial>   and    extern class Mesh<TGeometry, TMaterial> 

    var geometry : TGeometry; 

    var material : TMaterial;