You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: adev/src/context/best-practices.md
+14-2Lines changed: 14 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,23 @@
1
1
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.
2
2
3
3
## TypeScript Best Practices
4
+
4
5
- Use strict type checking
5
6
- Prefer type inference when the type is obvious
6
7
- Avoid the `any` type; use `unknown` when type is uncertain
7
8
8
9
## Angular Best Practices
10
+
9
11
- 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.
11
13
- Use signals for state management
12
14
- Implement lazy loading for feature routes
13
-
- Use `NgOptimizedImage` for all static images.
14
15
- 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.
15
18
16
19
## Components
20
+
17
21
- Keep components small and focused on a single responsibility
18
22
- Use `input()` and `output()` functions instead of decorators
19
23
- Use `computed()` for derived state
@@ -24,17 +28,25 @@ You are an expert in TypeScript, Angular, and scalable web application developme
24
28
- DO NOT use `ngStyle`, use `style` bindings instead
25
29
26
30
## State Management
31
+
27
32
- Use signals for local component state
28
33
- Use `computed()` for derived state
29
34
- Keep state transformations pure and predictable
30
35
- Do NOT use `mutate` on signals, use `update` or `set` instead
31
36
32
37
## Templates
38
+
33
39
- Keep templates simple and avoid complex logic
34
40
- Use native control flow (`@if`, `@for`, `@switch`) instead of `*ngIf`, `*ngFor`, `*ngSwitch`
35
41
- Use the async pipe to handle observables
36
42
37
43
## Services
44
+
38
45
- Design services around a single responsibility
39
46
- Use the `providedIn: 'root'` option for singleton services
40
47
- 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