Skip to content

gentype inconsistency with functions vs. react component #7466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jmagaram opened this issue May 12, 2025 · 2 comments
Open

gentype inconsistency with functions vs. react component #7466

jmagaram opened this issue May 12, 2025 · 2 comments

Comments

@jmagaram
Copy link
Contributor

See https://github.com/jmagaram/gentype-issue.

Look at TestModule.gen.tsx. Notice that the function result from the mathOperation is type a for the React component, which is a type error. There is no such problem with the makeRegular.

This is a simplified version of a real problem I'm having in my code. My React component is generic and a few of the props all rely on the same type param. I'm just a hobby programmer and run into problems with gentype. Does anyone actually use gentype in the real world? I'm really hoping the improvement in V12 happens with .d.ts file output.

export const make: React.ComponentType<{
  readonly initial: any;
  readonly mathOperation: (_1: any, _2: any) => a;
  readonly serialize: (_1: any) => string;
}> = TestModuleJS.make as any;

export const makeRegular: <a>(
  initial: a,
  mathOperation: (_1: a, _2: a) => a,
  serialize: (_1: a) => string,
) => string = TestModuleJS.makeRegular as any;

I tried this kind of .resi and the .gen.tsx does the right thing. BUT I can't figure out how to actually use it without a functor. So it is generic, but is a hassle to use.

type zebra

@react.component @genType
let make: (
  ~initial: zebra,
  ~mathOperation: (zebra, zebra) => zebra,
  ~serialize: zebra => string,
) => Jsx.element
@jmagaram
Copy link
Contributor Author

Defining the react component like this seems to work. But it isn't as ergonomic to use as the let make=(~initial,~mathOperation,... I was told of some kind of @react.component(: weirdProps) syntax but couldn't get it to work here.

type weirdProps<'a> = {
  initial: 'a,
  mathOperation: ('a, 'a) => 'a,
  serialize: 'a => string,
}

@react.component @genType
let make: (~p: weirdProps<'a>) => Jsx.element

@cometkim
Copy link
Member

cometkim commented May 15, 2025

This is definitely something that needs to be improved, but it's one of the hardest problems.

Type inference on polymorphic JSX props is a notorious problem even in the TypeScript world.

My advice in TypeScript is: Don't do it. But it's allowed in ReScript, so I'm not sure how to make it compatible. Using any seems reasonable to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants