Skip to content

Confusing type errors / warning when using optional record fields #5953

@ryyppy

Description

@ryyppy

Here's an example on different confusing errors / warnings:

type props = {count: int, username?: string}

let make = props => {
  // This is a pattern match, so it warns me with
  // "You forgot to handle a possible case here, for example: {count: _, username: None, _}"
  // As a user I would have expected `username` just to be a `option<string>` and that's it
  let {count, username} = props 

  // This typechecks, even though this shouldn't.
  // `username` should be an option<string>
  Js.String2.toLowerCase(username)->ignore

  // This pattern match warns me on missing case, since it thinks it's matching on `props`, not
  // on `props.username`:
  // "You forgot to handle a possible case here, for example: {count: _, username: None, _}"
  let _ = switch props.username {
  | Some("test") => "test"
  | _ => "whatever"
  }

  <div />
}

I think it all boils down to the fact that the initial props destructing doesn't really do what I actually expected.

Is this expected behavior?

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleOld issues that went stale

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions