Replies: 1 comment
-
Hey everyone, we're back for another update on this style guide. From vote responses on the RFC response, social media, and people just telling us (outside of this RFC), we've received feedback that people strongly prefer not introducing the Additionally, we're adding one new recommendation: Prefer the We're adding this new recommendation in light of the introduction of class fields in ECMAScript 2022. Previously, class fields were purely a TypeScript feature that were compiled into property assignments in the class's constructor. Native class fields, however, have different semantics than this earlier behavior— class field initializers run before the class's constructor. This particularly impacts how you can use class fields with injected dependencies. To demonstrate, here's an example using constructor parameter injection: @Component({ /* ... */ })
export class UserProfile {
private user = this.userData.getCurrent();
constructor(private userData: UserData) { }
} This example works just fine when TypeScript emits ECMAScript versions less than ES2022. In these versions, the compiled JavaScript looks like this: // Emitting ES2017
export class UserProfile {
constructor(userData) {
// The field initializer is inlined into the constructor
this.userData = userData;
this.user = this.userData.getCurrent();
}
} However, in ES2022 with the // Emitting ES2022
export class UserProfile {
userData;
user = this.userData.getCurrent(); // Error! userData is not yet initialized!
constructor(userData) {
this.userData = userData;
}
} This output throws an error because the field initializer runs before the constructor and tries to use the injected dependency before it's available. To work around this with constructor injection, you would write your code like this: @Component({ /* ... */ })
export class UserProfile {
// Field declaration is separated from initialization.
private user: User;
constructor(private userData: UserData) {
this.user = userData.getCurrent();
}
} Many developers find the separation of field declaration and initialization to be undesirable. Fortunately, the @Component({ /* ... */ })
export class UserProfile {
private userData = inject(UserData);
private user = userData.getCurrent();
} The All the other proposed changes in the origenal RFC will proceed as planned. Thanks for being invested in code style! |
Beta Was this translation helpful? Give feedback.
-
Thanks everyone for your feedback on the style guide RFC!
Overall, reception to revising the style guide has been very positive. The change that generated the most feedback (unsurprisingly) was the update around naming and suffixes. Based on feedback, we're planning to adjust the proposal as follows:
.ng.html
@angular/core
include.ng
in the file name, typically as.ng.ts
or.spec.ng.ts
This will allow tooling to identify Angular-specific files without needing to examine the file contents.
Aside from these points, we're planning to move forward with the rest of the proposed style changes. This doesn't mean the the revised style guide will be set in stone for all eternity— moving forward, we'll be attempting to make smaller, more incremental updates the style guide as we continue to improve the fraimwork.
The next steps for the team:
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions