Skip to content

Allow function type in untagged variants #6278

Closed

Description

@fhammerschmidt

Just tried to bind the MUI SxProps with untagged variants.

This is the TS definition:

export type SxProps<Theme extends object = {}> =
  | SystemStyleObject<Theme>
  | ((theme: Theme) => SystemStyleObject<Theme>)
  | ReadonlyArray<
      boolean | SystemStyleObject<Theme> | ((theme: Theme) => SystemStyleObject<Theme>)
    >;

and this is my attempt in ReScript:

module SxProps = {
  type theme = {primaryColor?: string}
  type systemStyleObject = {mr?: int, color?: string}

  @unboxed
  type rec t =
    | @as(false) False
    | @as(true) True
    | Object(systemStyleObject)
    | Array(array<t>)
    | Callback(callback)
  and callback = theme => systemStyleObject
}

which, aside from the fact that it's simplified because the array should not be nested and the booleans should only be nested, does not work, because one cannot use a function (the Callback variant in this example) type here.

Would it be possible to add that? Since you can check that easily with typeof == "function".

Activity

  1. cristianoc commented on May 31, 2023

    @cristianoc
    Collaborator

    Do you have references to example TS code that uses that?
    Just curious, not important. The description is already clear.

  2. added this to the v12 milestone on May 31, 2023
  3. fhammerschmidt commented on May 31, 2023

    @fhammerschmidt
    MemberAuthor
  4. cristianoc commented on May 31, 2023

    @cristianoc
    Collaborator

    Yes, here: https://mui.com/system/getting-started/the-sx-prop/#callback-values

    Thanks, so it looks like a case where one wants to construct values rather than doing pattern matching on.
    While the internal implementation of material UI is the one that does "pattern matching", in the sense of detecting different shapes used.

  5. fhammerschmidt commented on May 31, 2023

    @fhammerschmidt
    MemberAuthor

    That is true. Looks like in this case a bunch of externals with %identity is still the way to go.

    However, I still wonder if it would be a useful addition in other cases.

  6. zth commented on May 31, 2023

    @zth
    Member

    This pattern, providing either an object or a function to produce that object, is quite common in TS I'd say.

  7. cknitt commented on May 31, 2023

    @cknitt
    Member
        | Bool(bool)

    instead of

        | @as(false) False
        | @as(true) True

    would be nice, too. (Wasn't there a discussion about that somewhere else? Can't find the issue.)

  8. zth commented on May 31, 2023

    @zth
    Member
        | Bool(bool)

    instead of

        | @as(false) False
        | @as(true) True

    would be nice, too. (Wasn't there a discussion about that somewhere else? Can't find the issue.)

    Here: #6231

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions