diff --git a/README.md b/README.md index e147461..26e6f33 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ ts-generator supports: * Parenthesis optimization: They are placed only when they are needed to disambiguate. * Emitting either `null` or `undefined` for JVM nullable types. * Functions +* Constructors ## No Proper Way To Install For Now diff --git a/src/main/kotlin/me/ntrrgc/tsGenerator/TypeScriptGenerator.kt b/src/main/kotlin/me/ntrrgc/tsGenerator/TypeScriptGenerator.kt index 07b3e04..f1cd491 100644 --- a/src/main/kotlin/me/ntrrgc/tsGenerator/TypeScriptGenerator.kt +++ b/src/main/kotlin/me/ntrrgc/tsGenerator/TypeScriptGenerator.kt @@ -298,11 +298,35 @@ class TypeScriptGenerator( return "$typeKeyword ${klass.simpleName}$templateParameters$extendsString {\n" + + constructorsOf(klass) + propertiesOf(klass) + functionsOf(klass) + "}" } + private fun constructorsOf(klass: KClass<*>): String = try { + klass.constructors.joinToString("") { constructor -> + val parameters = constructor.parameters + .drop(1) + .joinToString(", ") { param -> + val paramType = pipeline.transformFunctionParameterType(param.type, param, constructor, klass) + "${param.name}: ${formatKType(paramType).formatWithoutParenthesis()}" + + } + val visibility = when (constructor.visibility) { + KVisibility.PRIVATE -> "private " + KVisibility.PROTECTED -> "protected " + KVisibility.PUBLIC -> "" + KVisibility.INTERNAL -> "" + else -> "" + } + " ${visibility}constructor($parameters)\n" + } + } catch (exception: kotlin.reflect.jvm.internal.KotlinReflectionInternalError) { + print(exception.toString()) + "" + } + private fun functionsOf(klass: KClass<*>): String = try { klass.declaredMemberFunctions .let { functionsList ->