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

Adjust type for render functions #277

Closed
ninanator opened this issue Mar 15, 2024 · 2 comments
Closed

Adjust type for render functions #277

ninanator opened this issue Mar 15, 2024 · 2 comments

Comments

@ninanator
Copy link
Contributor

Is your feature request related to a problem? Please describe.

When I use render, I have to write functions wrapping render to ensure that callers are putting in all of the expected fields for the templates. It would be helpful if the render* functions could be typed like:

export function render<T extends Record<string, any>>( // <- this could also be `T extends object` to retain the original typing
  this: Eta,
  template: string | TemplateFunction, // template name or template function
  data: T,
  meta?: { filepath: string }
):

so that, if desired, developers can either define the shape or fall back to Record<string, any>.

Describe the solution you'd like

I'd love to be able to define the render function like:

  const render = eta.render<MyType>('./template.eta', {
    ...fields for MyType...
  });

and if the types do not match, allow the compiler to yell at you. The developer would still be in charge of making sure their types and the templates match, but this would go a long way in helping ensure that any template changes are propagated appropriately across all usages in a project.

I'm willing to open a PR. Thank you!

@nebrelbug
Copy link
Collaborator

@ninanator thanks for the suggestion! I agree this would be helpful and will merge if you can open a PR. I think T extends object will likely be best so as not to break typing.

@ninanator
Copy link
Contributor Author

Completed by #278

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