Always explicitly declare optional types in interfaces and function parameters rather than relying on runtime checks or using `any`. This improves type safety, makes null/undefined handling requirements clear, and prevents runtime errors.
Always explicitly declare optional types in interfaces and function parameters rather than relying on runtime checks or using any. This improves type safety, makes null/undefined handling requirements clear, and prevents runtime errors.
Example:
// โ Poor - Implicit optionality, uses any
interface Props {
data: any;
config: any;
}
// โ
Good - Explicit optional types
interface Props {
data: DataType;
config?: ConfigType; // Explicitly optional
extraction?: Extraction; // Clearly shows optionality
}
Benefits:
When implementing components or functions with optional parameters, ensure the types accurately reflect which values could be undefined. This creates a contract that makes null handling requirements obvious to consumers.
Enter the URL of a public GitHub repository