8000 Angular expressions should be plain TypeScript expressions · Issue #43485 · angular/angular · GitHub
[go: up one dir, main page]

Skip to content

Angular expressions should be plain TypeScript expressions #43485

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
8000
petebacondarwin opened this issue Sep 17, 2021 · 6 comments
Open

Angular expressions should be plain TypeScript expressions #43485

petebacondarwin opened this issue Sep 17, 2021 · 6 comments
Labels
area: compiler Issues related to `ngc`, Angular's template compiler canonical This issue represents a canonical design issue in Angular. feature Issue that requests a new feature
Milestone

Comments

@petebacondarwin
Copy link
Contributor
petebacondarwin commented Sep 17, 2021

Which @angular/* package(s) are relevant/releated to the feature request?

compiler

Description

This issue is a container for all the issues that have requested new expression syntax features.
The aim is to collect all the features into one place so that we can track the work.
The plan is to do an holistic analysis of the syntax and decide things like:

  1. How close to ECMAScript syntax we want to stay
  2. What are the security implications of exposing particular syntaxes
  3. How to incorporate Angular specific syntax, such as pipes and structural directive microsyntax.

The result will be a project proposal for the changes that we would like to make to the syntax in the short to medium term.

Related Issues

  1. feat(compiler): Support "in" operator in Angular templates #38560
  2. Align with the optional chaining spec #34385
  3. Pipe (|) operator has incorrect precedence (too tight) #27415
  4. Support arrow functions in template syntax #14129
  5. Proposal: Allow computed property names in map expressions #13855
  6. Use the spread operator in template #11850
  7. Allow bigint literals in templates #44852
@samuelfernandez
Copy link

Could this enhancement for the compiler be included too? #43253

@JoostK
Copy link
Member
JoostK commented Oct 16, 2021

Could this enhancement for the compiler be included too? #43253

That appears to be unrelated to Angular's expression syntax, as it's a request for more accurate type-checking. So we'll keep that one separate.

@RobertAKARobin
Copy link

Really looking forward to seeing this completed! Not having as local-val, and having instead to use *ngIf, is a big turn-off for developers considering Angular or ramping up on Angular projects.

@ghost
Copy link
ghost commented Aug 26, 2022

(edited for grammar errors)

I hope this helps. There are some cases where features like arrow functions could be used instead of features like EventEmitters; thus, Angular's API surface can be reduced because the @Output() decorator and (event) = "" syntax could be substituted and treated as input props/attributes, Here is an example:
HTML:

- <button (click)="fire($event)">Click me!<button>
+ <button [onclick]="fire">Click me!<button> <!-- where `fire` is an arrow function that accepts the native event object -->

Component:

- @Output() onIncr = EventEmitter<number>()
+ @Input() onIncr: (x: number) => void

Now that I think about it, I think @Input() could be changed to something like @Prop().

Treating outputs as inputs can also allow more output parameters per attribute, shallow out the learning curve, and facilitate spread props (see #14545), especially with multiple wrapping children.

Regarding pipes, I don't think they're necessary as (I think) Angular is built on the concept of separation of concerns (i.e. templates are for light to no computations, and components and services are for medium to heavy computations), and removing the pipe operator will also decrease the API surface and remove two tokens to check for (: and |). By the way, according to TC39, there is a pipe operator (|>) being proposed for the ECMAScript standard (they may be out already because GH is not showing any syntax errors below). I saw a few examples on MDN a few years ago, which is how I found out about this potential feature, that look similar to this:

5 |> x2 |> console.log // => 10

However, I can no longer find the examples on MDN.

@jessicajaniuk jessicajaniuk added the canonical This issue represents a canonical design issue in Angular. label Sep 22, 2022
@ghost
Copy link
ghost commented Oct 7, 2022

I hope this helps. There are some cases where features like arrow functions could be used instead of features like EventEmitters; thus, Angular's API surface can be reduced because the @output() decorator and (event) = "" syntax could be substituted and treated as input props/attributes,

Ok, I can see why my comment got a thumbs down. Without the (banana) syntax, there is not a way to implement two-way binding unless devs are okay with thiis:

<input [ngModel]="name" [onNgModelChange]="newName => (name = newName)">

Maybe the banana syntax could translate to the example above:

<input (ngModel)="name">

That will need further discussion.

@ghost
Copy link
ghost commented Oct 7, 2022

On a separate note, I like the structural directive micro-syntax a lot; at first, it made think that *ngFor="let item of items" was being translated to for (let item of items) { ... } during compilation, which I think is why devs try to write *ngFor="let item in items". I wonder if the micro-syntax could be implemented in regular directives as well? For example, this:

<input [ngModel]="name; onChange: newName => (name = newName)">

could be mapped to this:

<input [ngModel]="name" [ngModelOnChange]="newName => (name = newName)">

...under the hood.

@alxhub alxhub changed the title feat(compiler): improvements to the Angular expression syntax Angular expressions should be plain TypeScript expressions May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: compiler Issues related to `ngc`, Angular's template compiler canonical This issue represents a canonical design issue in Angular. feature Issue that requests a new feature
Projects
None yet
Development

No branches or pull requests

5 participants
0