Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ryyppy opened this issue Jan 25, 2023 · 7 comments
Closed

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

ryyppy opened this issue Jan 25, 2023 · 7 comments
Labels
stale Old issues that went stale

Comments

@ryyppy
Copy link
Member

ryyppy commented Jan 25, 2023

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?

@cknitt
Copy link
Member

cknitt commented Jan 25, 2023

This is indeed expected behavior. You would need to do

let {count, ?username} = props

This is documented here: https://rescript-lang.org/docs/manual/latest/record#pattern-matching-on-optional-fields

I agree that it feels quite unexpected when destructuring. It tripped me up, too, when I first encountered it.

@zth
Copy link
Collaborator

zth commented Jan 25, 2023

Maybe we can help out here and automatically insert ? via the editor tooling now that we have autocomplete in destructuring...?

@zth
Copy link
Collaborator

zth commented Jan 25, 2023

We should also probably document an actual destructure, the docs has switches only.

@zth
Copy link
Collaborator

zth commented Jan 25, 2023

Fixed in the editor tooling in rescript-lang/rescript-vscode#715

@ryyppy
Copy link
Member Author

ryyppy commented Jan 26, 2023

IMO if this is the only way an optional attr should be destructured, then it should be enforced on the type checker level. Everything else seems hacky to me.

@cristianoc
Copy link
Collaborator

This is not about optional args. You can as well write let 3 = x with similar outcome.

Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale Old issues that went stale label Sep 19, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Old issues that went stale
Projects
None yet
Development

No branches or pull requests

4 participants