-
Notifications
You must be signed in to change notification settings - Fork 45
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
Support for sealed classes #4
Comments
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 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. |
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. |
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. |
Would be nice to see sealed class to union type support, e.g.
Would compile to:
The text was updated successfully, but these errors were encountered: