Skip to content
Latchkey

F# "error FS0001: This expression was expected to have type" in CI

F#'s type inference found an expression whose type does not match its context. FS0001 prints the expected and actual types and points at the subexpression that disagrees, failing the build.

What this error means

dotnet build fails with "error FS0001: This expression was expected to have type X but here has type Y".

F#
Program.fs(7,18): error FS0001: This expression was expected to have type
    'string'
but here has type
    'int'

Common causes

A value of the wrong type is supplied

The expression is one type (here int) but the context needs another (string); F# does not convert numeric and string types implicitly.

Inference fixed a type earlier than intended

An earlier use constrained a binding to a type that conflicts with a later use, so FS0001 fires at the second site.

How to fix it

Convert or annotate the value

  1. Read the expected versus actual type in the message.
  2. Insert the right conversion (for example string n) or add a type annotation.
  3. Re-run dotnet build.
Program.fs
let label = "id-" + string n

Add annotations to steer inference

Annotate the parameter or binding so inference fixes the intended type rather than one implied by an earlier use.

Program.fs
let format (n: int) : string = string n

How to prevent it

  • Annotate public function signatures so inference is predictable.
  • Build locally before pushing to catch FS0001 early.
  • Convert between types explicitly; F# avoids implicit coercion.

Related guides

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