gleam-lang/gleam

let bindings are typechecked using their inferred type not their (narrowed) annotated type

Open

#4,642 opened on May 30, 2025

View on GitHub
 (2 comments) (7 reactions) (0 assignees)Rust (960 forks)batch import
help wanted

Repository metrics

Stars
 (21,417 stars)
PR merge metrics
 (Avg merge 10d 19h) (69 merged PRs in 30d)

Description

When a let binding produces a type error because of its annotation, the rest of the code is type-checked with the actual inferred type of the binding instead of the (possibly narrowed) annotated type. This potentially produces misleading follow-up type errors:

pub fn main() {
  let x: String = 5  // type error: expected String, got Int
  let y: Int = x     // valid
  let z: String = x  // type error: expected String, got Int
}

I think the assignment to y should produce the type error here, not z. I would have expected one of the major benefits of adding explicit annotations to be to help the compiler produce better errors in exactly those situations. My assumption here of course is that if the user adds an explicit annotation, they want and expect that annotation to be assumed the correct type.

~ 💜

Contributor guide