diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/export/ExportModelToTsDeclarations.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/export/ExportModelToTsDeclarations.kt index edd16530ef9..13464287e52 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/export/ExportModelToTsDeclarations.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/export/ExportModelToTsDeclarations.kt @@ -66,6 +66,13 @@ fun ExportedDeclaration.toTypeScript(indent: String): String = indent + when (th val membersString = members.joinToString("") { it.toTypeScript("$indent ") + "\n" } + // If there are no exported constructors, add a private constructor to disable default one + val privateCtorString = + if (!isInterface && !isAbstract && members.none { it is ExportedConstructor }) + "$indent private constructor();\n" + else + "" + val renderedTypeParameters = if (typeParameters.isNotEmpty()) "<" + typeParameters.joinToString(", ") + ">" @@ -74,7 +81,9 @@ fun ExportedDeclaration.toTypeScript(indent: String): String = indent + when (th val modifiers = if (isAbstract && !isInterface) "abstract " else "" - val klassExport = "$modifiers$keyword $name$renderedTypeParameters$superClassClause$superInterfacesClause {\n$membersString$indent}" + val bodyString = privateCtorString + membersString + indent + + val klassExport = "$modifiers$keyword $name$renderedTypeParameters$superClassClause$superInterfacesClause {\n$bodyString}" val staticsExport = if (nestedClasses.isNotEmpty()) "\n" + ExportedNamespace(name, nestedClasses).toTypeScript(indent) else "" klassExport + staticsExport } diff --git a/js/js.translator/testData/typescript-export/constructors/constructors.d.ts b/js/js.translator/testData/typescript-export/constructors/constructors.d.ts index 0aa55b64c22..66590dc03e7 100644 --- a/js/js.translator/testData/typescript-export/constructors/constructors.d.ts +++ b/js/js.translator/testData/typescript-export/constructors/constructors.d.ts @@ -9,10 +9,12 @@ declare namespace JS_TESTS { readonly x: string; } class ClassWithSecondaryCtor { + private constructor(); readonly x: string; static create(y: string): ClassWithSecondaryCtor } class ClassWithMultipleSecondaryCtors { + private constructor(); readonly x: string; static createFromString(y: string): ClassWithMultipleSecondaryCtors static createFromInts(y: number, z: number): ClassWithMultipleSecondaryCtors @@ -24,6 +26,7 @@ declare namespace JS_TESTS { static createFromInts(y: number, z: number): OpenClassWithMixedConstructors } class DerivedClassWithSecondaryCtor extends OpenClassWithMixedConstructors { + private constructor(); static delegateToPrimary(y: string): DerivedClassWithSecondaryCtor static delegateToCreateFromInts(y: number, z: number): DerivedClassWithSecondaryCtor }