mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2026-04-16 22:24:49 +00:00
7.2 KiB
7.2 KiB
applyTo
| applyTo |
|---|
| **/*.ts |
TypeScript Development Standards
Type Safety
- Strict Type Checking: Always enable and adhere to strict type checking. This helps catch errors early and improves code quality.
- Prefer Type Inference: Allow TypeScript to infer types when they are obvious from the context. This reduces verbosity while maintaining type safety.
- Avoid
any: Do not use theanytype unless absolutely necessary as it bypasses type checking. Preferunknownwhen a type is uncertain and you need to handle it safely. - Use strict null checks (no
nullorundefinedwithout explicit handling) - Use type guards and union types for robust type checking
- Check for missing return types in function signatures
- Avoid implicit
any(untyped function parameters)
Naming Conventions
- Use PascalCase for types, interfaces, and classes
- Use camelCase for variables, functions, and methods
- Use UPPER_CASE for constants
Modern TypeScript Patterns
- Use optional chaining (
?.) and nullish coalescing (??) - Prefer
constoverlet; never usevar - Use arrow functions for callbacks and short functions
- Avoid enums - they generate additional code at compile time, which increases the size of the final file. This can have a negative impact on the loading speed and performance of the app. Prefer union types or literal types instead.
- Avoid unhandled promise rejections (missing .catch() or try/catch)
- Use proper async/await pattern
- Avoid inefficient array operations (e.g., nested .map())
- Use destructuring for object/array access
- Prefer arrow functions
Angular Best Practices
- Standalone Components: Always use standalone components, directives, and pipes. Avoid using
NgModulesfor new features or refactoring existing ones. - Implicit Standalone: When creating standalone components, you do not need to explicitly set
standalone: trueinside the@Component,@Directiveand@Pipedecorators, as it is implied by default. - Lazy Loading: Implement lazy loading for feature routes to improve initial load times of your application.
- Use Angular Material or other modern UI libraries for consistent styling and UI components.
- Implement proper error handling with RxJS operators (e.g., catchError)
- Verify if newly added functionalities can utilize Angular Signals for fine-grained reactivity, reducing change detection overhead.
- Utilize AOT (Ahead-of-Time) compilation and tree-shaking for efficient, smaller bundle sizes.
- Prefer class binding over
ngClassandngStylefor better performance. - Use protected on class members that are only used by a component's template, as it allows for better encapsulation while still being accessible to the template.
- Use readonly for properties that shouldn't change.
- Use
takeUntilDestroyed&destroyRef: ThetakeUntilDestroyedanddestroyRefhave been introduced with Angular 16 and help to reduce boilerplate code related to unsubscribing on theOnDestroyhook. - Organize the order of properties and methods in Angular components for readability and maintainability. Recommended order is:
- Injected services Whether they are public or private, it's clear they are dependencies of the class.
- Inputs: Properties that receive data from outside.
- Outputs: Events that the component can trigger.
- ViewChild/ContentChild: References to HTML elements.
- Public static properties: Constants and static members that are accessible to everyone.
- Readonly properties: Immutable public properties.
- Public properties: Data and functions available to everyone.
- Private static properties: Constants and static members that are only accessible within the class.
- Private readonly properties: Immutable private properties.
- Private properties: Data and functions used only inside the component.
- Setters and Getters: Methods for accessing and modifying properties.
- Constructor: Used to initialize the component.
- Lifecycle Hooks: Methods that run at specific times in the component’s lifecycle.
- Public methods: Functions available to everyone.
- Private methods: Functions used only inside the component.
Components
- Single Responsibility: Keep components small, focused, and responsible for a single piece of functionality.
- Reactive Forms: Prefer Reactive forms over Template-driven forms for complex forms, validation, and dynamic controls due to their explicit, immutable, and synchronous nature.
- Use Typed Forms: Typed Forms in Angular are a new feature introduced in Angular 14 that provide stronger type checking for reactive forms. They allow developers to define the structure and types of form controls, making it easier to catch errors at compile-time rather than runtime.
Services
- Single Responsibility: Design services around a single, well-defined responsibility.
providedIn: 'root': Use theprovidedIn: 'root'option when declaring injectable services to ensure they are singletons and tree-shakable.inject()Function: Prefer theinject()function over constructor injection when injecting dependencies, especially withinprovidefunctions,computedproperties, or outside of constructor context.
Unit testing
- Write unit tests for components, services, and pipes using Jasmine and Karma.
- Test cases should be reasonably groupped based on tested functionality/behaviour using describe blocks.
- Use plain English test names based on the should when pattern as a guideline.
- Use Angular's TestBed for component testing with mocked dependencies
- Avoid Direct Calls to Component Lifecycle Hooks: Instead of directly invoking lifecycle hooks like
ngOnInit(), use Angular's testing utilities to trigger them naturally. For example, usefixture.detectChanges()to trigger change detection, which will automatically callngOnInit()and other lifecycle hooks in the correct order. - Use fixture.componentRef.setInput() Instead of Direct Input Assignment: When testing components with inputs, use
fixture.componentRef.setInput()to set input values. This method ensures that Angular's change detection is properly triggered, allowing the component to react to input changes as it would in a real application. - Use the Provide Mock Store for testing components that rely on NgRx state management. This allows you to mock the store and control the state during tests without needing to set up a full NgRx environment.
- Mock HTTP requests using provideHttpClientTesting
- Import only the minimal required modules
- Avoid NO_ERRORS_SCHEMA and CUSTOM_ELEMENTS_SCHEMA in tests to ensure proper error detection
- Do not verify mocked methods
- Avoid mocking component methods unless necessary; prefer testing actual behavior
- Avoid testing private methods directly; test them through public methods instead
- Avoid testing methods or behaviours of children components; use shallow testing or mock child components instead
- Use the overrideProviders API to replace components, directives, pipes, or services declared deep within the module hierarchy
- Avoid async/await in synchronous unit tests
- Prefer data-automation-id over CSS class when possible
- Do not use toBeDefined() to check if an element is visible.