diff --git a/src/main/kotlin/no/iktdev/ts/MapStrategy.kt b/src/main/kotlin/no/iktdev/ts/MapStrategy.kt new file mode 100644 index 0000000..c39df42 --- /dev/null +++ b/src/main/kotlin/no/iktdev/ts/MapStrategy.kt @@ -0,0 +1,6 @@ +package no.iktdev.ts + +enum class MapStrategy { + MapAsPartialRecord, + MapAsRecord +} \ No newline at end of file diff --git a/src/main/kotlin/no/iktdev/ts/TsModelRenderer.kt b/src/main/kotlin/no/iktdev/ts/TsModelRenderer.kt index dc1ff7a..8561ece 100644 --- a/src/main/kotlin/no/iktdev/ts/TsModelRenderer.kt +++ b/src/main/kotlin/no/iktdev/ts/TsModelRenderer.kt @@ -1,6 +1,8 @@ package no.iktdev.ts +import no.iktdev.ts.anno.TsMapStrategy import kotlin.reflect.KClass +import kotlin.reflect.full.findAnnotation import kotlin.reflect.full.memberProperties import kotlin.reflect.full.primaryConstructor @@ -111,10 +113,17 @@ class TsModelRenderer { .filter { it.name !in inheritedPropNames } .filter { forcedTypeLiteral == null || it.name != "type" } .joinToString("\n") { prop -> + val mapStrategy = prop.findAnnotation()?.value + ?: MapStrategy.MapAsRecord + val tsType = if (prop.name == "type" && defaultTypeLiteral != null) { "\"$defaultTypeLiteral\"" } else { - TsTypeMapper.kotlinToTsType(prop.returnType.toString(), typeParams) + TsTypeMapper.kotlinToTsType( + prop.returnType.toString(), + typeParams, + mapStrategy + ) } " ${prop.name}: $tsType;" } diff --git a/src/main/kotlin/no/iktdev/ts/TsTypeMapper.kt b/src/main/kotlin/no/iktdev/ts/TsTypeMapper.kt index ebc0601..9262e47 100644 --- a/src/main/kotlin/no/iktdev/ts/TsTypeMapper.kt +++ b/src/main/kotlin/no/iktdev/ts/TsTypeMapper.kt @@ -1,7 +1,11 @@ package no.iktdev.ts object TsTypeMapper { - fun kotlinToTsType(kotlinType: String, genericParams: List = emptyList()): String { + fun kotlinToTsType( + kotlinType: String, + genericParams: List = emptyList(), + mapStrategy: MapStrategy = MapStrategy.MapAsRecord, + ): String { val isNullable = kotlinType.endsWith("?") // Fjerner eventuelle backticks som Kotlin legger rundt lokale/test-klasser, og trimmet val clean = kotlinType.removeSuffix("?").replace("`", "").trim() @@ -26,7 +30,7 @@ object TsTypeMapper { return if (isNullable) "string | null" else "string" } - // Map → Record + // Map → Record / Partial> if (clean.startsWith("kotlin.collections.Map") || clean.startsWith("kotlin.collections.MutableMap")) { @@ -36,7 +40,13 @@ object TsTypeMapper { val tsKey = kotlinToTsType(key, genericParams) val tsValue = kotlinToTsType(value, genericParams) - val base = "Record<$tsKey, $tsValue>" + val record = "Record<$tsKey, $tsValue>" + + val base = when (mapStrategy) { + MapStrategy.MapAsRecord -> record + MapStrategy.MapAsPartialRecord -> "Partial<$record>" + } + return if (isNullable) "$base | null" else base } diff --git a/src/main/kotlin/no/iktdev/ts/anno/TsMapStrategy.kt b/src/main/kotlin/no/iktdev/ts/anno/TsMapStrategy.kt new file mode 100644 index 0000000..ba9bf4e --- /dev/null +++ b/src/main/kotlin/no/iktdev/ts/anno/TsMapStrategy.kt @@ -0,0 +1,9 @@ +package no.iktdev.ts.anno + +import no.iktdev.ts.MapStrategy + +@Target(AnnotationTarget.PROPERTY) +@Retention(AnnotationRetention.RUNTIME) +annotation class TsMapStrategy( + val value: MapStrategy +) \ No newline at end of file