-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaybeResult.ts
More file actions
27 lines (22 loc) · 927 Bytes
/
MaybeResult.ts
File metadata and controls
27 lines (22 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import type { ProblemDetailError } from './ProblemDetail.ts'
export type Result<T> = { result: T }
export type ProblemResult = { error: ProblemDetailError }
export type MaybeResult<T> = ProblemResult | Result<T>
export const isResult = <T>(result: MaybeResult<T>): result is Result<T> =>
!('error' in result)
export const isProblem = <T>(result: MaybeResult<T>): result is ProblemResult =>
'error' in result
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
export function assertResult(
result: MaybeResult<unknown>,
): asserts result is Result<unknown> {
if (!isResult(result))
throw new TypeError('Expected result to be a Result<T> type')
}
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
export function assertProblem(
result: MaybeResult<unknown>,
): asserts result is ProblemResult {
if (!isProblem(result))
throw new TypeError('Expected result to be a ProblemResult type')
}