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

Discussion for a new feature - Optional Type Specification #160

Open
cshaa opened this issue Jan 19, 2025 · 1 comment
Open

Discussion for a new feature - Optional Type Specification #160

cshaa opened this issue Jan 19, 2025 · 1 comment

Comments

@cshaa
Copy link

cshaa commented Jan 19, 2025

😧 The Problem

My team maintains a moderately large Adonis application and we rely on type safety to catch bugs during refactoring. However, there is no kind of type checking for views – to check whether one passes the correct data to a view, one has to manually cross-compare the Edge file with the type in the controller. What's worse, one has to infer the type of the properties from their usage – and if they're passed into a subcomponent, one has to check the subcomponent. This adds overhead to our development process.

πŸ†οΈ High-Level Solution

1. Add an optional way to specify component's props (This feature request)

The amount of work one needs to do (in order to check that props in the view match the props in the controller) would significantly decrease if there was a standard way to denote all props a component takes. This props section could be entirely optional in order to keep backwards compatibility and to respect users that don't want type safety πŸ˜…

2. Add typechecking to view.render in Adonis (Future feature request)

Then, it wouldn't be too hard to add a command into Adonis, which would take all the prop types and generate a type definition for view.render (similar to astro sync or sveltekit sync). Then, TypeScript would make sure that developers can only pass properties of the same type that is declared in the Edge file.

3. Add typechecking to Edge's language server (Future feature request)

Last, and potentially the most difficult step, would be to integrate the type information into the language server / VS Code extension, so that the developer would get proper intellisense for props, and errors where they use them in an incorrect way.

🚲️ Syntax / Bikeshedding

There are two important goals the syntax for describing props should have:

  1. The types should be valid TypeScript. Rationale: Maintaining custom syntax for types would be a lot of work, and typechecking with TS would be harder.
  2. It should be possible to compile Edge files without including a TypeScript parser. Rationale: TypeScript's type syntax is complicated (making the parser large/slow) and new versions often introduce new syntax (adding maintanance work to parser developers).

As long as these two goals are met, almost anything goes.

A nice syntax, that would be almost trivial for Edge to implement, is using Edge tags:

@props
  name: string;
  greeting?: string;
@end

The contents of the prop tag can be anything that is permitted inside interface Props { ... }.

@thetutlage
Copy link
Member

This seems like a great proposal and I think should be fairly simple to implement as well. However, the type-safety will only be for the TypeScript codebase all the internal usage of props within Edge templates will be untyped. This is because, Edge doesn't have a language server to impose type-safety

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