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

Diagnose it: which runtime is on PATH?

Terminal
which -a <runtime>
<runtime> --version
cat .tool-versions .nvmrc .ruby-version .python-version 2>/dev/null

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.

Frequently asked questions

What causes Kotlin "Type mismatch" in CI?
There are 3 common causes: nullable value where non-null required, wrong type supplied, and generic inference picked a wider type. A String? (often from a platform type or a map lookup) is passed where a non-null String is required.
How do I fix Kotlin "Type mismatch" in CI?
There are 2 fixes depending on which cause you have: handle the nullability explicitly and correct or convert the type. Work through them in order, since the first is the most common.
What does Kotlin "Type mismatch" in CI actually mean?
The build fails with Type mismatch: inferred type is X?
How do I stop Kotlin "Type mismatch" in CI happening again?
Annotate platform types from Java interop. The prevention section lists 3 changes that keep it from recurring.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card