exclusive-or-properties
Use Optional Never Properties to Model Exclusive Or
Overview
Sometimes you need a type where exactly one of several properties must be present, but not more than one. This "exclusive or" (XOR) pattern is common in component props, API parameters, and configuration objects. Using optional properties with never types enforces this constraint at compile time.
This technique provides better type safety than unions of objects and clearer intent than runtime checks.
When to Use This Skill
- Exactly one of several properties must be present
- Modeling mutually exclusive configuration options
- Component props with alternative configurations
- API parameters that have exclusive variants
- Preventing invalid combinations of properties
The Iron Rule
Use optional never properties to enforce "exactly one of" constraints. Each variant makes its property required and others never.
More from marius-townhouse/effective-typescript-skills
precise-any-variants
Use when forced to use any. Use when any is too broad. Use when function types need any.
86narrow-any-scope
Use when any is unavoidable. Use when working with untyped libraries. Use when silencing specific type errors.
35tsdoc-comments
Use when documenting public APIs. Use when writing library code. Use when using JSDoc-style comments. Use when generating documentation. Use when explaining complex types.
34exhaustiveness-checking
Use when handling tagged unions. Use when adding new cases to discriminated unions. Use when switch statements must cover all cases.
13code-gen-independent
Use when confused about types at runtime. Use when trying to use instanceof with interfaces. Use when type errors don't prevent JavaScript output.
12tsconfig-options
Use when setting up a TypeScript project. Use when confused by type checking behavior. Use when strict mode causes unexpected errors.
11