Skip to content

RFC: @deriving for labeled arguments #6530

Closed as not planned
Closed as not planned

Description

@cometkim

Motivation

JavaScript doesn't support labeled arguments, but ReScript does. So let add: (~a, ~b) in ReScript compiled to function add(a, b), so the labels a and b cannot be referenced by the call site.

In JS/TS codebase it is common to use object destructuring for this (e.g. props in React components) Also called RORO pattern

Until v10, gentype created a runtime mapper for this. However, since v11 it no longer generates runtime mappers. In the long term, gentype shouldn't have any runtime (See #6196)

However, the runtime mapper for props is an important feature for interoperability not only with TypeScript but also with JavaScript codebases in general. There must be an alternative to completely removing the feature.

This proposal solves the problem by extending the @deriving tag existing in the ReScript core.

Detailed Design

Basic

@genType
@deriving(params)
let add = (~a, ~b) => a + b

produces

type \"add$Params" = {
  a: int,
  b: int,
}

@genType
let \"add$with" = ({ a, b }: \"add$Params") => {
  let add = add(~a, ~b)
  add
}

This is only valid for functions with labeled arguments, and requires that the @genType tag be duplicated if it exists.

High-order functions

@deriving(params) can only be used on top-level functions. This is because new type bindings cannot be defined in a closure. Btw, user may want mappers for nested functions like:

@deriving(params)
let add = (~a, ~b) => {
  (~c, ~d) => {
    (e, f) => {
      a + b + c + d + e + f
    }
  }
}

If the return type of the function annotated by @deriving(params) is a function, then it should produces

type \"add$Params" = {
  a: int,
  b: int,
}

type \"add$Params$1" = {
  c: int,
  d: int,
}

@genType
let \"add$with" = ({ a, b }: \"add$Params") => {
  let add = add(~a, ~b)
  ({ c, d }: \"add$Params$1") => {
    let add = add(~c, ~b)
    add
  }
}

Deriving is applied in nested functions, but ends when it encounters a function with no labeled parameters.

Customizing definitions

User can customize derived typenames to make it more useful in TypeScript side.

@deriving({ params: "add(Props)" })
let add = (~a, ~b) => a + b
type \"Props" = {
  a: int,
  b: int,
}

let add = ({ a, b }: \"Props") => {
  let add = add(~a, ~b)
  add
}

Implementation

TBD

Activity

  1. cometkim commented on Dec 20, 2023

    @cometkim
    MemberAuthor

    @DZakh noticed another pattern related to this

    The props mapping is sometimes used even within higher-order functions. But this proposal does not handle the case, for example:

    let fn = (~a, ~b) => {
      (~c, ~d) => {
        a + b + c + d
      }
    }

    How the ppx generate types and mappers for this?


    Updated the RFC to handle this case

  2. cometkim commented on Dec 20, 2023

    @cometkim
    MemberAuthor

    Another issue is the genType doesn't reflect PPX's results at all. I think it should


    It can be done by PPX implementor, see #6537

  3. cometkim commented on Dec 28, 2023

    @cometkim
    MemberAuthor

    Implementation is currently blocked by #6539

  4. changed the title [-]RFC: `@deriving` for labeled arguments[/-] [+][RFC] `@deriving` for labeled arguments[/+] on Jan 3, 2024
  5. changed the title [-][RFC] `@deriving` for labeled arguments[/-] [+]RFC: `@deriving` for labeled arguments[/+] on Apr 2, 2024
  6. moved this to Todo in RFCson Nov 28, 2024
  7. github-actions commented on May 22, 2025

    @github-actions

    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.

  8. added
    stale
    on May 22, 2025
  9. removed
    stale
    on May 22, 2025
  10. github-actions commented on May 18, 2026

    @github-actions

    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.

  11. added
    stale
    on May 18, 2026
  12. moved this from Todo to Done in RFCson May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stale

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions