Writing Angular code with an AI assistant is like pair programming with someone who knows everything, but only if you ask the right questions.
This guide will walk you through the best way to prompt LLMs for Angular apps and components, using expert approved TypeScript and Angular practices.
TypeScript and Angular Best Practices
So here’s a checklist of best practices one should expect the LLM to follow. Use these to inform the prompt and verify the output.
TypeScript Best Practices
Use
stricttype checking
The prompt should always assume the project usesstrict: trueintsconfig.json.Prefer type inference when obvious
Let TypeScript do the work where possible no need to clutter code with redundant types.Avoid
any; useunknowninstead
anykills type safety. If you don’t know the type yet, ask forunknown+ type guards.
Angular Best Practices
Components
- Always use standalone components (No need of
standalone: true; it’s implicit). - Use signals and
computed()for state management. - Keep components small and single-responsibility.
- Use
input()andoutput()functions instead of decorators @input and @output. - Set
changeDetection: ChangeDetectionStrategy.OnPush. - Prefer inline templates for small components.
-
Avoid
ngClassandngStyleuse[class]and[style]bindings instead. - Prefer Reactive Forms over template driven ones.
Services
- Design services with single responsibility.
- Use
providedIn: 'root'for singleton services. - Use the
inject()function, not constructor injection.
State & Templates
- Use signals for local state.
- Use
computed()for derived values. - Keep state pure and predictable.
- In templates, avoid complex logic.
- Use native control flow (
@if,@for,@switch) over structural directives like*ngIf,*ngFor,*ngSwitch. - Use the
asyncpipe for observables.
Assets & Routing
- Use
NgOptimizedImagefor all static images. - Use lazy loading for all feature routes.
When Reviewing LLM Generated Code
LLMs can hallucinate or use outdated patterns. When reviewing the code, always check for:
- ✅ Use of
signalsoverRxJSunless necessary - ✅
computed()for derived state - ✅ Proper use of
inject()in services - ✅ Inline templates for simple components
- ✅ Avoidance of
*ngIf,*ngForin favor of@if,@for - ✅ No
ngClassorngStyle
Happy prompting
Top comments (0)