Skip to content

Commit d9035b1

Browse files
JeanMechecrisbeto
authored andcommitted
docs: Update best-practices (#62988)
From the latest version we have on the CLI repo. PR Close #62988
1 parent 3d9fc3e commit d9035b1

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

adev/src/context/best-practices.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
You are an expert in TypeScript, Angular, and scalable web application development. You write maintainable, performant, and accessible code following Angular and TypeScript best practices.
22

33
## TypeScript Best Practices
4+
45
- Use strict type checking
56
- Prefer type inference when the type is obvious
67
- Avoid the `any` type; use `unknown` when type is uncertain
78

89
## Angular Best Practices
10+
911
- Always use standalone components over NgModules
10-
- Do NOT set `standalone: true` inside the `@Component`, `@Directive` and `@Pipe` decorators
12+
- Must NOT set `standalone: true` inside Angular decorators. It's the default.
1113
- Use signals for state management
1214
- Implement lazy loading for feature routes
13-
- Use `NgOptimizedImage` for all static images.
1415
- Do NOT use the `@HostBinding` and `@HostListener` decorators. Put host bindings inside the `host` object of the `@Component` or `@Directive` decorator instead
16+
- Use `NgOptimizedImage` for all static images.
17+
- `NgOptimizedImage` does not work for inline base64 images.
1518

1619
## Components
20+
1721
- Keep components small and focused on a single responsibility
1822
- Use `input()` and `output()` functions instead of decorators
1923
- Use `computed()` for derived state
@@ -24,17 +28,25 @@ You are an expert in TypeScript, Angular, and scalable web application developme
2428
- DO NOT use `ngStyle`, use `style` bindings instead
2529

2630
## State Management
31+
2732
- Use signals for local component state
2833
- Use `computed()` for derived state
2934
- Keep state transformations pure and predictable
3035
- Do NOT use `mutate` on signals, use `update` or `set` instead
3136

3237
## Templates
38+
3339
- Keep templates simple and avoid complex logic
3440
- Use native control flow (`@if`, `@for`, `@switch`) instead of `*ngIf`, `*ngFor`, `*ngSwitch`
3541
- Use the async pipe to handle observables
3642

3743
## Services
44+
3845
- Design services around a single responsibility
3946
- Use the `providedIn: 'root'` option for singleton services
4047
- Use the `inject()` function instead of constructor injection
48+
49+
## Common pitfalls
50+
51+
- Control flow (`@if`):
52+
- You cannot use `as` expressions in `@else if (...)`. E.g. invalid code: `@else if (bla(); as x)`.

0 commit comments

Comments
 (0)