Type Safety
Compile-time prevention of value mixing. Never accidentally pass a User ID where a Product ID is expected, or mix up quantities.
Kotlin Multiplatform library that prevents mixing incompatible values at compile time

In any application, mixing up values of the same primitive type is a common source of bugs:
// 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!With Typed-Value, the compiler catches these mistakes:
// 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!Add the dependency to your build.gradle.kts:
kotlin {
sourceSets {
commonMain {
dependencies {
implementation("com.ekino.oss:typed-value-core:1.0.0")
}
}
}
}dependencies {
implementation("com.ekino.oss:typed-value-core:1.0.0")
}Then define your typed values:
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 | Platform | Description |
|---|---|---|
typed-value-core | JVM, JS, Native | Core TypedValue types with zero dependencies |
typed-value-jackson | JVM | JSON serialization/deserialization |
typed-value-spring | JVM | Spring MVC path variable & request param converters |
typed-value-hibernate | JVM | JPA/Hibernate abstract entities, converters & repository support |
typed-value-querydsl | JVM | Type-safe QueryDSL expressions + Q-classes for Hibernate entities |
typed-value-spring-data-elasticsearch | JVM | Elasticsearch document mapping |