Skip to content
Type mismatch: inferred type is ... but ... was expected

Type mismatch: inferred type is ... but ... was expected

DodaTech 3 min read

The “Type mismatch” error appears when the Kotlin compiler detects that a value’s type does not match the expected type at a given position in your code.

What It Means

Kotlin is a statically typed language. Every expression has a type determined at compile time, and the compiler verifies that types align across assignments, function arguments, return values, and operators. When the inferred type differs from the expected type, compilation stops with a “Type mismatch” error.

Why It Happens

  • You assign a value of one type to a variable of another incompatible type.
  • A function returns a type different from its declared return type.
  • You pass an argument whose type doesn’t match the function parameter.
  • You need an explicit type conversion (e.g., Int to Long, Double to Int).
  • You are trying to use a smart cast but the compiler cannot guarantee the type.
  • A nullable type is used where a non-null type is expected.

How to Fix It

1. Use explicit type conversions

// ❌ Type mismatch: inferred type is Int but Long was expected
val number: Long = 42

// ✅ Convert Int to Long explicitly
val number: Long = 42L
// or
val number: Long = 42.toLong()

2. Fix function return types

// ❌ Type mismatch: inferred type is String but Int was expected
fun getLength(text: String): Int {
    return text // returning a String, not an Int
}

// ✅ Return the correct type
fun getLength(text: String): Int {
    return text.length
}

3. Cast with as? for safe downcasting

fun process(obj: Any) {
    // ❌ Type mismatch: String? cannot be converted to String
    val text: String = obj as String

    // ✅ Safe cast with nullable handling
    val text: String? = obj as? String
    text?.let { println(it.length) }
}

4. Convert numeric types explicitly

fun calculate(rate: Double, years: Int): Double {
    // ❌ Type mismatch: Int to Double
    val result = rate * years // years is Int, rate is Double — works via coercion
    return result
}

// If you need an explicit conversion:
fun calculate(rate: Double, years: Int): Double {
    val result = rate * years.toDouble()
    return result
}

5. Use the correct collection type

// ❌ Type mismatch: MutableList<Int> vs List<Int>
fun process(items: List<Int>) {
    items.add(1) // List is read-only, no add()
}

// ✅ Use MutableList if you need to modify
fun process(items: MutableList<Int>) {
    items.add(1)
}

6. Handle nullable types in conditionals

// ❌ Type mismatch: String? vs String
fun greet(name: String?) {
    val greeting: String = "Hello, " + name
}

// ✅ Use safe call or Elvis
fun greet(name: String?) {
    val greeting: String = "Hello, " + (name ?: "Guest")
}
What does 'inferred type' mean in Kotlin?
Kotlin infers types from context. When you write val x = 42, the compiler infers Int. If you then assign x to a Long variable, the inferred type (Int) doesn’t match the expected type (Long). The error message shows both types so you can see the mismatch.
Can I disable type checking in Kotlin?
No — type safety is a core feature. You can use as? (safe cast) or Any as an escape hatch, but these bypass compile-time checks and shift errors to runtime. Prefer explicit conversions and proper typing.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro