TypeScript Best Practices
Follow the repository's compiler settings, style, supported runtime, and public API conventions before applying fallback preferences.
Principles
- Prefer inference for local implementation and explicit types at meaningful contracts.
- Validate untrusted runtime data; a type assertion does not make data safe.
- Model valid states and exhaustive branches instead of widening types to suppress errors.
- Preserve literal precision with the narrowest appropriate mechanism.
- Use generics only when they express a real relationship.
- Avoid
any, unchecked double assertions, wrapper object types, and duplicate source-of-truth types. - Add type-level tests for reusable or subtle type contracts.
Read only the relevant reference for the problem:
references/setup.mdreferences/code-style.mdreferences/type-patterns.mdreferences/union-exhaustive.mdreferences/type-testing.mdreferences/branded-types.mdreferences/template-literals.md
Verify with the repository's actual TypeScript and test configuration.