Skip to content

Commit 09e8d22

Browse files
committed
hack: nullable type in template
1 parent 5677190 commit 09e8d22

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/main/kotlin/me/ntrrgc/tsGenerator/TypeScriptGenerator.kt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,22 @@ class TypeScriptGenerator(
152152
}
153153

154154

155-
private fun formatKType(kType: KType): TypeScriptType {
155+
private fun formatKType(kType: KType, isInTypeConstraint: Boolean = false): TypeScriptType {
156156
val classifier = kType.classifier
157157
if (classifier is KClass<*>) {
158158
val existingMapping = predefinedMappings[classifier]
159159
if (existingMapping != null) {
160-
return TypeScriptType.single(predefinedMappings[classifier]!!, kType.isMarkedNullable, voidType)
160+
// When in a type constraint, we shouldn't add the nullable union type
161+
return TypeScriptType.single(
162+
predefinedMappings[classifier]!!,
163+
kType.isMarkedNullable && !isInTypeConstraint,
164+
voidType
165+
)
161166
}
162167
if (!shouldIgnoreSuperclass(classifier) && !isSameClass(classifier, klass))
163168
dependentTypes.add(classifier)
164169
}
165170

166-
167171
val classifierTsType =
168172
if (classifier is KClass<*>) {
169173
predefinedMappings.getOrDefault(
@@ -186,9 +190,11 @@ class TypeScriptGenerator(
186190
else
187191
"UNKNOWN" // giving up
188192

189-
return TypeScriptType.single(classifierTsType, kType.isMarkedNullable, voidType)
193+
// When in a type constraint, we shouldn't add the nullable union type
194+
return TypeScriptType.single(classifierTsType, kType.isMarkedNullable && !isInTypeConstraint, voidType)
190195
}
191196

197+
192198
private fun nonPrimitiveFromKType(kType: KType): String {
193199
val kClass = kType.classifier as KClass<*>
194200
val binaryName = kClass.binaryName()
@@ -207,15 +213,16 @@ class TypeScriptGenerator(
207213
return binaryName + if (kClass.typeParameters.isNotEmpty()) "<${
208214
(1..kClass.typeParameters.size).joinToString(
209215
", "
210-
) { "Any" }
216+
) { "Object" }
211217
}>" else ""
212218
}
213219

214220
// Only add generic parameters if counts match
215221
return binaryName + if (kType.arguments.isNotEmpty()) {
216222
"<" + kType.arguments.joinToString(", ") { arg ->
217223
formatKType(
218-
arg.type ?: KotlinAnyOrNull
224+
arg.type ?: KotlinNotNull,
225+
true
219226
).formatWithoutParenthesis()
220227
} + ">"
221228
} else ""
@@ -324,7 +331,8 @@ class TypeScriptGenerator(
324331
val bounds = typeParameter.upperBounds
325332
typeParameter.name + if (bounds.isNotEmpty()) {
326333
" extends " + bounds.joinToString(" & ") { bound ->
327-
formatKType(bound).formatWithoutParenthesis()
334+
// Pass true for isInTypeConstraint
335+
formatKType(bound, true).formatWithoutParenthesis()
328336
}
329337
} else {
330338
""
@@ -335,6 +343,7 @@ class TypeScriptGenerator(
335343
}
336344

337345

346+
338347
return "$typeKeyword ${klass.binaryName()}$templateParameters$extendsString{\n" +
339348
staticFieldsOf(klass) +
340349
(if (klass.java.isInterface) "" else staticMethodsOf(klass, interfaceSupertypes)) +
@@ -695,6 +704,7 @@ class TypeScriptGenerator(
695704

696705
companion object {
697706
private val KotlinAnyOrNull = Any::class.createType(nullable = true)
707+
private val KotlinNotNull = Any::class.createType(nullable = false)
698708

699709
fun isJavaBeanProperty(kProperty: KProperty<*>, klass: KClass<*>): Boolean {
700710
val beanInfo = Introspector.getBeanInfo(klass.java)

0 commit comments

Comments
 (0)