ts-exhaustive-match-plugin
v0.0.8
Published
TypeScript Language Service Plugin for generating exhaustive pattern matching
Downloads
6
Maintainers
Readme
ts-exhaustive-match-plugin
A TypeScript Language Service Plugin that generates exhaustive pattern matching for discriminated union types and string literal unions with zero dependencies.
Features
✨ Automatic Exhaustive Pattern Generation: Generates complete if-else chains with TypeScript's satisfies never for compile-time exhaustiveness checking
🎯 Multiple Integration Points:
- Refactoring: Right-click on identifiers to generate exhaustive matches
- Auto-completion: Type-ahead support for discriminated union identifiers and property access
- Quick Fix: Add missing cases to incomplete pattern matches with a single click
- IntelliSense: Works seamlessly in VS Code, WebStorm, and other TypeScript-enabled editors
🔧 Comprehensive Support:
- Function parameters (regular, async, methods, arrow functions)
- Variable declarations (const, let)
- Standalone identifier expressions
- If-statement contexts with intelligent completion
- Type narrowing support (works within type guards)
🏷️ Smart Discriminant Detection: Automatically detects tag property as discriminant (configurable in future versions)
Supported Union Types
Discriminated Unions
type Result =
| { tag: "success"; data: string }
| { tag: "error"; message: string }
const result: Result = getResult()
// These cases will get generated:
if (result.tag === "success") {
} else if (result.tag === "error") {
} else {
result satisfies never
}String Literal Unions
type Status = "loading" | "success" | "error"
const status: Status = getStatus()
// These cases will get generated:
if (status === "loading") {
} else if (status === "success") {
} else if (status === "error") {
} else {
status satisfies never
}Supported Contexts
The plugin generates exhaustive matches in these contexts:
// Function parameters
function handle(result: Result) {
// Cursor on 'result' → generates exhaustive match
}
// Variable declarations
const result: Result = getResult()
// Cursor on 'result' → generates exhaustive match
// Standalone expressions
result // Cursor here → generates exhaustive match
// If statements with smart completion
if (result. // Auto-completes with exhaustive match for discriminated unions
if (status // Auto-completes with exhaustive match for string literal unionsInstallation
Install the plugin as a development dependency:
npm install --save-dev ts-exhaustive-match-pluginConfiguration
Add the plugin to your tsconfig.json:
{
"compilerOptions": {
"plugins": [
{
"name": "ts-exhaustive-match-plugin"
}
]
}
}Usage
Via Refactoring (Right-click menu)
- Place your cursor on a discriminated union identifier
- Right-click and select "Refactor..."
- Choose "Generate exhaustive match"
Via Auto-completion
- Type a discriminated union identifier followed by
. - Select the exhaustive match completion from the suggestion list
- Tab through the generated placeholder blocks
Via Quick Fix
When you have an incomplete pattern match:
type Status = "idle" | "pending" | "done"
const status: Status = getStatus()
if (status === "idle") {
// Handle idle
} else {
status satisfies never // TypeScript error: Type '"pending" | "done"' does not satisfy 'never'
}- Place your cursor on the
satisfies neverexpression - Use the quick fix shortcut (Cmd+. on Mac, Ctrl+. on Windows/Linux)
- Select "Add missing cases: pending, done"
- The plugin will automatically add the missing cases
Generated Output
For a discriminated union like:
type ApiResponse =
| { tag: "loading" }
| { tag: "success"; data: User[] }
| { tag: "error"; error: string }
function handleResponse(response: ApiResponse) {
// Generated exhaustive match:
if (response.tag === "loading") {
// Handle loading
} else if (response.tag === "success") {
// Handle success - data is typed as User[]
} else if (response.tag === "error") {
// Handle error - error is typed as string
} else {
response satisfies never // Ensures exhaustiveness
}
}Editor Support
- ✅ VS Code: Full support with IntelliSense and refactoring
- ✅ WebStorm/IntelliJ: Works with TypeScript Language Service
- ✅ Vim/Neovim: Via TypeScript LSP clients
- ✅ Emacs: Via TypeScript mode
Requirements
- TypeScript 5.0 or higher
- A TypeScript-enabled editor or IDE
Future Enhancements
- Configurable discriminant property names
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT
