Skip to content

[NotForNow] GenType: explore the idea of using namespaces to represent modules. #6117

Closed as not planned
@cristianoc

Description

@cristianoc

Explore a cleaner way to represent ReScript modules in TypeScript using namespaces

Background

In ReScript, modules are used to organize code and encapsulate related types and values. ReScript modules can contain values (such as functions) and type declarations, allowing users to access them using the syntax M.t for a type t defined inside module M. Modules can also be nested, and ReScript supports functors (functions between modules).

Currently, genType maps ReScript modules to TypeScript constructs, but there might be a cleaner way to represent them using TypeScript namespaces.

Proposed Solution

The proposed solution is to represent ReScript modules and their features in TypeScript using namespaces:

  1. Representing ReScript modules: Use TypeScript namespaces to group related types and values.

    namespace M {
      export type t = {
        // Your type definition goes here
      };
    
      export function someFunction(): t {
        // Your function implementation goes here
      }
    }
  2. Representing nested ReScript modules: Use nested TypeScript namespaces.

namespace M {
  export type t = {
    // Your type definition goes here
  };

  export function foo(): t {
    // Your function implementation goes here
  }

  export namespace N {
    export type t = {
      // Your type definition goes here
    };

    export function bar(x: M.t): t {
      // Your function implementation goes here
    }
  }
}
  1. Accessing a namespace defined in another file: Export the namespace from the source file and then import it in the file where you want to access it.
// Somefile.ts
export namespace SomeModule { /* ... */ }

// AnotherFile.ts
import { SomeModule } from './Somefile';
  1. Mimicking ReScript functors: Use higher-order functions that take objects representing namespaces as input and return objects representing namespaces.
// Define functor-like higher-order function
function myFunctor(input: typeof InputNamespace): typeof OutputNamespace { /* ... */ }

// Use the functor-like higher-order function
const outputModule = myFunctor(InputNamespace);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions