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

Allow the Context type to be passed a Component type to infer props #233

Closed
brainkim opened this issue Jul 16, 2022 · 1 comment
Closed
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@brainkim
Copy link
Member

brainkim commented Jul 16, 2022

Currently, the first type parameter to the Context class is the props for the component:

function *Greeting(this: Context<{name: string}>, {name}: {name: string}) {
  let count = 0;
  for ({name} of this) {
    yield <div>Hello, {name} {count++}</div>;
  }
}

Unfortunately, this leads to the situation where we have to type the props twice, once for the Context, and once for the props parameter. TypeScript uses the type of the first parameter in JSX, so it’s important that the props are typed. Nevertheless, it’s annoying to have to either type out the props twice, or to extract the props in a separate type.

I was playing around in the TypeScript playground, and you can actually reference the function type of the function you’re currently defining in its parameters. So, I had the idea that, rather than passing the props to the current context, you could pass the component type itself, and infer the props. This works:

interface Context<TComponent extends (this: Context<never>, props: never) => unknown> {
  props: Parameters<TComponent>[0];
  [Symbol.iterator](): Generator<Parameters<TComponent>[0]>;
}

function *Greeting(this: Context<typeof Greeting>, {name}: {name: string}) {
  let count = 0;
  for ({name} of this) {
    yield <div>Hello, {name} {count++}</div>;
  }
}

The this: Context<typeof Greeting> seems like a more or less acceptable amount of syntax for the user, and it allows props to be defined inline. Unfortunately, this requires us to change the TypeScript API, so it would be a breaking change. We could define another type name that does the same thing, but I’m feeling like ripping off the band-aid, as this is only a TypeScript thing and wouldn’t have an effect on the runtime. Plus I can’t think of a name for the alternative type.

Something to think about.

@brainkim brainkim self-assigned this Jul 16, 2022
@brainkim brainkim added the enhancement New feature or request label Jul 16, 2022
@brainkim brainkim added this to the 0.5.0 milestone Oct 5, 2022
@brainkim brainkim changed the title Making the Context type more useful Allow the Context type to be passed a Component type to infer props Nov 17, 2022
@brainkim
Copy link
Member Author

brainkim commented Feb 1, 2023

Added in 0.5.0

@brainkim brainkim closed this as completed Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant