rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

Missing type import in generated TypeScript File with genType for React Components

Open WhyThat opened this issue 1 year ago • 0 comments

Hey!

I've identified an issue with genType with React component

Explaination

I have two components which are globally the same

// Working.res
type t = string
type props = {children: React.element, t: t}
@genType
let make = ({children}: props) => {
  <div> children </div>
}

And

// NotWorking.res
type not_imported = string

type props = {children: React.element, not_imported: not_imported}

@genType
let make = ({children}: props) => {
  children
}

When compiling these components the types generated by @genType are good :ok_hand:

But if I want to export then from another file like that:

// index.res
@genType
let \"NotWorking" = NotWorking.make

@genType
let \"Working" = Working.make

The index.gen.tsx generated by genType is:

/* TypeScript file generated from index.res by genType. */

/* eslint-disable */
/* tslint:disable */

import * as indexJS from './index.res.js';

import type {Jsx_element as PervasivesU_Jsx_element} from '../src/shims/PervasivesU.shim';

import type {props as NotWorking_props} from './NotWorking.gen';

import type {props as Working_props} from './Working.gen';

export type workaroundProps = NotWorking_props;

export const NotWorking: React.ComponentType<{ readonly children: React.ReactNode; readonly not_imported: not_imported }> = indexJS.NotWorking as any;

As you can see the type not_imported is not imported

Reproduction

A minimal reproduction example is available in this GitHub repository: gentype-error-reproduction. It outlines the setup and demonstrates the specific steps needed to observe the problematic behavior.

Workaround

By redefining the props within the index.res file as follows, I found a temporary solution:

type workaroundProps = NotWorking.props
@genType let \"Workaround": workaroundProps => React.element = NotWorking.make

This approach forces a correct import of types in the generated TypeScript file, avoiding the issue temporarily.

Question

Is the identified workaround the recommended approach to this issue, or does it highlight a bug within genType that needs addressing?

Have a great day :)

WhyThat avatar Feb 09 '24 14:02 WhyThat