Giter VIP home page Giter VIP logo

Comments (3)

ntrrgc avatar ntrrgc commented on August 28, 2024

The problem is that TypeScript unions and sealed class don't map as much as I'd like.

The most striking difference is that sealed class are abstract classes, whereas named unions are not, and therefore they need to be omitted in the extends clause.

Also, sealed children may be defined either as a nested classes or separate classes in the same file (since Kotlin 1.1). Unfortunately there is no way to query sealed children in the later case.

from ts-generator.

bernerbrau avatar bernerbrau commented on August 28, 2024

OK, so, check my PR - turns out sealed classes are more complicated than I initially thought. It also generates code (in a separate output property) to check if a JS object has a matching type; when working with union types this seemed necessary to implement. I did have to refactor the class visitor pattern a bit to make this work.

It may be more than you're looking for, and I haven't included unit tests, but it seems to work well on our Kotlin class model. I can add in some UTs if that helps.

It's true that there's no way using vanilla Kotlin or Java reflection to scan for subclasses. So I used the Reflections library to scan for subclasses; Reflections does some basic classpath scanning to accomplish this.

I'm also working on a gradle plugin to generate this on the fly for mixed-sourceset Kotlin and Node projects and/or multimodule projects.

from ts-generator.

bernerbrau avatar bernerbrau commented on August 28, 2024

I've added in UTs for two basic typedef cases. I had to refactor the factory code that builds TypeScriptDefinitions since the union type made it much more complex.

The actual generated code that does type checking still needs UTs.

from ts-generator.

Related Issues (20)

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.