Kotlin "Type mismatch" in CI
The Kotlin compiler expected one type and inferred another. Often a nullability difference: a nullable value supplied where a non-null type is required.
What this error means
The build fails with Type mismatch: inferred type is X? but X was expected (or two unrelated types), pointing at the expression. It is deterministic.
gradle
e: file:///src/main/kotlin/App.kt:12:24 Type mismatch:
inferred type is String? but String was expectedCommon causes
Nullable value where non-null required
A String? (often from a platform type or a map lookup) is passed where a non-null String is required.
Wrong type supplied
The expression genuinely produces an unrelated type from what the position expects.
Generic inference picked a wider type
Inference settled on a supertype (Any, a common bound) that does not satisfy the specific required type.
How to fix it
Handle the nullability explicitly
Provide a non-null value with a default, a check, or a safe call.
App.kt
val name: String = config["name"] ?: "default"Correct or convert the type
- Confirm the required type at the call site.
- Convert the expression to it, or fix the source value.
- Add an explicit type argument when generic inference widens.
How to prevent it
- Annotate platform types from Java interop.
- Avoid !! except where null is truly impossible.
- Compile locally so type errors surface before CI.
Related guides
Kotlin "Unresolved reference" variant in CIFix Kotlin "Unresolved reference" errors in Gradle CI for member access and extension functions - the named s…
Kotlin "Smart cast is impossible" in CIFix the Kotlin "Smart cast to X is impossible, because the value is a mutable / open property" compile error…
sbt "type mismatch; found X required Y" in CIFix the Scala "type mismatch; found : X required: Y" compile error in sbt CI builds where an expression produ…