Skip to content
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

Prop types are not inferred if component is returned indirectly. #68

Open
chronoDave opened this issue Aug 5, 2022 · 2 comments
Open
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@chronoDave
Copy link
Contributor

chronoDave commented Aug 5, 2022

When using TypeScript, prop types are not correctly inferred if the forgo component is not directly returned, see minimal example

image

@chronoDave
Copy link
Contributor Author

chronoDave commented Aug 5, 2022

As far as I know you can't pass props into variables in TypeScript, so to me this seems like a "gotcha!" that could be mentioned in the documentation perhaps?

@spiffytech
Copy link
Member

Currently, this is how you have to do it.

I'm not happy with this, but we couldn't find a way make TypeScript infer the types if the generic was specified on only one of the two types. We discussed alternate component syntaxes that got around this, but they all came with other tradeoffs.

We'd gladly accept a PR that fixes this 🙂 I'm indifferent two which of the two types would get the annotation, and which would be inferred.

Am I safe to assume that the type in ForgoNewComponentCtor is directly passed into forgo.Component and if so, is it still necessary to use ForgoNewComponentCtor?

Yep, the type is passed directly in:

export type ForgoNewComponentCtor<Props extends {} = {}> = (
  props: Props & ForgoComponentProps
) => Component<Props>;

The ForgoNewComponentCtor type solves two needs: the constructor receives the same props that are passed to the component's first render, and it lets TypeScript warn you when you forget to return a Component instance. If you don't care about those, you don't need to use the type.

As far as I know you can't pass props into variables in TypeScript, so to me this seems like a "gotcha!" that could be mentioned in the documentation perhaps?

Good call, I'll update the migration section of the docs to reflect this.

@spiffytech spiffytech added enhancement New feature or request good first issue Good for newcomers labels Aug 9, 2022
spiffytech added a commit that referenced this issue Aug 29, 2022
#68: Update the docs to clarify handling component generic types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants