React TypeScript
Priority: P1 (OPERATIONAL)
Type-safe React patterns.
Implementation Guidelines
- Components: Prefer interface/type (
Props) overReact.FC(which has implicit children). UseJSX.ElementorReactNodeas the return type. - Children: For components that accept children, use
PropsWithChildren<T>or explicitly type them asReact.ReactNode. - Events: Always type event handlers using specific React events, such as
React.ChangeEvent<HTMLInputElement>orReact.FormEvent<HTMLFormElement>. - Hooks: For
useRef, avoidany; useuseRef<HTMLDivElement>(null). ForuseState, use generics for complex types:useState<User | null>(null). - Native Elements: Use
ComponentPropsWithoutRef<'button'>orComponentPropsWithRefto extend native attributes safely. - Generics: Implement generic components for reusable UI like lists using
<T,>(props: ListProps<T>). - Discriminated Unions: Use Discriminated Unions for mutually exclusive props (e.g.,
successvserrorstates). - Utility Types: Leverage
Omit,Pick, andPartialto transform prop interfaces and avoid redundancy.
Anti-Patterns
- No
any: Useunknown. - No
React.FC: Implicit children is deprecated/bad practice. - No
Function: Use(args: T) => void.
References
See references/example.md for typed props, generic components, and hook ref patterns.