Skip to content
Latchkey

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 expected

Common 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

  1. Confirm the required type at the call site.
  2. Convert the expression to it, or fix the source value.
  3. 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →