Skip to content

Pattern match on an Record<>'s key. #25

@guyzmo

Description

@guyzmo

Is your feature request related to a problem? Please describe.

I have an object which type is alike the following:

  • const type MyErrorType = Record<string, {message: string}>

the record's key is the name of a form element, which is typed like an enum, e.g. for a form with inputs "firstname, lastname, email", the type would be alike:

  • const type MyErrorType = Record<"firstname"|"lastname"|"email", {message: string}>

For a real world use case, it's the errors object returned by "react-hook-form", for which I wish to use pattern matching.

Today I can pattern match on the keys I know how to deal with, e.g.:

    match(errors) // errors: MyErrorType
      .with(
        {firstname: {message: select()}},
        {lastname: {message: select()}}, (res) => console.error(`Name error: ${res}`))

But I have no elegant solution to match the keys I'm not sure how to deal with.

Describe the solution you'd like

Something I believe would be elegant, would be to have a pattern match destructuring on the object's key, maybe something looking like:

      .with({[select("key")]: {message: select("message")}}, ({key,message}) => console.error(`${key}: ${message}`)})

Describe alternatives you've considered

Today the only solution I found is:

      .with(__, (res) => {const keys = Object.keys(res); console.error(`${keys[0]}: ${res[keys[0]]?.message ?? "is invalid"}`)})

Additional context

I have RTFM the manual, looked through the issues, but I might have missed something better than my alternative. I also did try my solution, and it was skipped straight to the otherwise().

About the use case, I'm trying to pattern match within react-hook-form's handleSubmit second argument (that contains the form errors), which type is DeepMap<T, FieldErrors> where T is the type of the form, and FieldErrors a structure containing information about an error for a form field.

P.S.: Thank you for this work, this is something I missed a lot when doing frontend dev 😉

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions