https://github.com/freckle/exhaustive-js
Provides a helper function for checking exhaustiveness.
https://github.com/freckle/exhaustive-js
ghvm-managed
Last synced: about 1 year ago
JSON representation
Provides a helper function for checking exhaustiveness.
- Host: GitHub
- URL: https://github.com/freckle/exhaustive-js
- Owner: freckle
- License: mit
- Created: 2022-04-08T15:53:25.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-04-05T02:23:41.000Z (about 1 year ago)
- Last Synced: 2025-05-07T09:51:51.557Z (about 1 year ago)
- Topics: ghvm-managed
- Language: TypeScript
- Homepage:
- Size: 45.9 KB
- Stars: 0
- Watchers: 12
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# @freckle/exhaustive-js
Provides a helper function for checking exhaustiveness. Exhaustiveness checking is a feature of a language where the type checker guarantees that all cases were covered.
## Usage
```js
import {exhaustive} from '@freckle/exhaustive-js'
type Status = 'success' | 'failure'
function getStatusMessage(status: Status): string {
switch (status) {
case 'success':
return 'The operation completed successfully.'
case 'failure':
return 'The operation failed.'
default:
return exhaustive(status)
}
}
```
Usage in a reducer where returning the state itself in the default case is a common pattern:
```js
import {exhaustiveReducer} from '@freckle/exhaustive-js'
type TodoState = {...}
type TodoAction = {type: 'todoAdded', ...} | {type: 'todoToggled', ...}
const defaultTodoState = {...}
function todoReducer(
state: TodoState = defaultTodoState,
action: TodoAction
): TodoState {
switch (action.type) {
case 'todoAdded':
return {...}
case 'todoToggled':
return {...}
default:
return exhaustiveReducer(action.type, state)
}
}
```