You call `myFunction("x")` and the compiler error reads `expected SessionToken but found String`, yet `SessionToken` is a typealias. Why does the error use the alias name, and does that change the type semantics?
answer
- Alias name is display sugar in errors
- Type checking uses expanded type
- Same-underlying-type messages can't truly clash
- Jump-to-declaration to see real type
- Confusion usually = alias resolves elsewhere
basics
~20 sThe alias name shows up in messages because the compiler prints the name you wrote for readability. But it is still the same underlying type — the alias does not change semantics, only how the type is displayed.
solid answer
~50 sThe error surfaces the **alias name** because Kotlin tries to print types using the name that appears in source, which is friendlier than always expanding to the underlying type. It is purely a **presentation** choice — semantically `SessionToken` *is* its underlying type after expansion, so there is no type mismatch caused by the alias itself. If you see `expected SessionToken but found String` and `SessionToken = String`, then something else is wrong (e.g. you actually have a different alias or a genuine type difference), because a real `String` would satisfy a `String`-backed alias. Often such a message means the alias resolves to a **different** type than you assumed (e.g. `typealias SessionToken = ByteArray`). The fix is to inspect the alias declaration to learn its real underlying type. Aliases never introduce new typing rules; they only affect how the compiler *names* types in diagnostics and IDE hovers.
go deeper
Understands the alias name in the message is just a label, not a separate type.
Knows type checking uses the expanded type and that the message hints the alias resolves elsewhere.
Uses jump-to-declaration / expansion as a systematic debugging step and explains the readability heuristic.
Considers how alias-rich APIs affect diagnostic clarity and team onboarding, balancing naming vs transparency.
## Why the alias name appears Kotlin's compiler and IDE prefer to **display** a type using a name that exists in your source. When a `typealias` is in scope for the type, diagnostics, `:type` hints, and hover tooltips often show the **alias name** rather than the fully expanded form, because it is shorter and more meaningful. ```kotlin typealias SessionToken = ByteArray fun login(t: SessionToken) { /* ... */ } login("abc") // error mentions SessionToken, not ByteArray ``` The message *reads* `SessionToken` but the **rule** being enforced is the underlying one: `ByteArray` was required, `String` was supplied — a real mismatch. ## It does NOT change semantics - Type checking always uses the **expanded** (underlying) type. - The alias name is **cosmetic** in messages. - A value of the underlying type always satisfies the alias and vice versa. So if `SessionToken = String`, a `String` argument is *accepted*; you would never get `expected SessionToken but found String` in that case. Seeing such a message is a strong signal that the alias resolves to a **different** underlying type than you assumed. ## Debugging recipe 1. **Jump to the alias declaration** (Ctrl/Cmd-click the alias) and read its right-hand side — the real underlying type. 2. Mentally **expand** it and re-read the error. 3. Confirm whether the two sides truly differ; if they are the same underlying type, the error must be about something else (nullability, variance, a different alias with the same name imported, etc.). ## Takeaway Aliases affect **diagnostics presentation and readability**, never the **typing rules**. When an alias-flavored error confuses you, expand it to the underlying type to reason precisely.
- If `SessionToken = String`, could you ever get 'expected SessionToken but found String'?No. A String satisfies a String-backed alias, so such a message means the alias actually resolves to a different underlying type.
- Why does the IDE sometimes show the alias and sometimes the expanded type?It is a readability heuristic; expanded forms appear when no alias is in scope or when expansion clarifies the type (e.g. for inference output).
saying these in an interview costs you the question
- Believing the alias name changes what the compiler accepts
- Assuming the displayed alias must equal what you thought it aliases
- Not checking the alias's right-hand side when confused
- Thinking diagnostics imply a new nominal type exists
- Treating the alias as a wrapper that needs unwrapping