Skip to content

Typed-ValueType-safe Values

Kotlin Multiplatform library that prevents mixing incompatible values at compile time

Typed-Value Logo

The Problem

In any application, mixing up values of the same primitive type is a common source of bugs:

kotlin
// Classic bug: passing wrong value
fun getOrder(orderId: String): Order { ... }
fun getUser(userId: String): User { ... }

val userId = "user-123"
val order = getOrder(userId) // Compiles, but wrong!

// Another classic bug: mixing quantities
fun addToCart(productId: Long, quantity: Int) { ... }
addToCart(quantity = 5, productId = 42) // Oops, swapped arguments!

The Solution

With Typed-Value, the compiler catches these mistakes:

kotlin
// Type-safe identifiers
fun getOrder(orderId: TypedString<Order>): Order { ... }
fun getUser(userId: TypedString<User>): User { ... }

val userId = "user-123".toTypedString<User>()
val order = getOrder(userId) // Compilation error!

// Type-safe quantities
class Banana
class Apple
val bananas = 5.toTypedInt<Banana>()
val apples = 3.toTypedInt<Apple>()

fun addBananas(count: TypedInt<Banana>) { ... }
addBananas(apples) // Compilation error!

Quick Start

Add the dependency to your build.gradle.kts:

kotlin
kotlin {
    sourceSets {
        commonMain {
            dependencies {
                implementation("com.ekino.oss:typed-value-core:1.0.0")
            }
        }
    }
}
kotlin
dependencies {
    implementation("com.ekino.oss:typed-value-core:1.0.0")
}

Then define your typed values:

kotlin
import com.ekino.oss.typedvalue.*

// Type-safe identifiers
class User
class Product
val userId = "user-123".toTypedString<User>()
val productId = 42L.toTypedLong<Product>()

// Type-safe quantities
class Banana
class Apple
val bananas = 5.toTypedInt<Banana>()
val apples = 3.toTypedInt<Apple>()

// Type-safe money
class Cents
val price = 1999L.toTypedLong<Cents>()

// Use in your domain model
data class CartItem(
    val productId: TypedLong<Product>,
    val quantity: TypedInt<Product>,
    val priceInCents: TypedLong<Cents>
)

Module Overview

ModulePlatformDescription
typed-value-coreJVM, JS, NativeCore TypedValue types with zero dependencies
typed-value-jacksonJVMJSON serialization/deserialization
typed-value-springJVMSpring MVC path variable & request param converters
typed-value-hibernateJVMJPA/Hibernate abstract entities, converters & repository support
typed-value-querydslJVMType-safe QueryDSL expressions + Q-classes for Hibernate entities
typed-value-spring-data-elasticsearchJVMElasticsearch document mapping

Get started with the full guide →

Released under the MIT License.