Type Definitions
Reference for all types exported by @anilkumarthakur/match.
Types
Handler<T>
Handler function type for match expression results.
type Handler<T> = () => TA function that takes no parameters and returns a value of type T.
Type Parameter:
T- The return type
Example:
const handler: Handler<string> = () => 'result'
const numHandler: Handler<number> = () => 42Predicate<T>
Predicate function type for guard/conditional matching.
type Predicate<T> = (value: T) => booleanA function that takes a subject value and returns a boolean indicating whether the match condition is satisfied.
Type Parameter:
T- The type of the subject being matched
Example:
const isPositive: Predicate<number> = (n) => n > 0
const isString: Predicate<unknown> = (v) => typeof v === 'string'
const isLongString: Predicate<string> = (s) => s.length > 10
// Used with match
match(10)
.on(isPositive, () => 'Positive')
.otherwise(() => 'Not positive')
// Inline predicates
match(score)
.on(
(n) => n >= 90,
() => 'A'
)
.on(
(n) => n >= 80,
() => 'B'
)MatcherHandler<T>
Internal handler type (same as Handler<T>).
export type MatcherHandler<T> = () => TNote: This is for internal use. Use Handler<T> instead.
MatchChain<TSubject, TResult>
Interface for match chain operations.
export interface MatchChain<TSubject, TResult> {
on: (
pattern: TSubject | Predicate<TSubject>,
handler: Handler<TResult>
) => MatchChain<TSubject, TResult>
otherwise: (handler: Handler<TResult>) => TResult
}Type Parameters:
TSubject- The type of values being matchedTResult- The return type of handler functions
Properties:
on()- Add a single caseotherwise()- Set default and execute
Example:
import { match, MatchChain } from '@anilkumarthakur/match'
const chain: MatchChain<string, number> = match<string, number>('test').on('test', () => 42)Classes
Matcher<TSubject, TResult>
The core matcher class.
class Matcher<TSubject, TResult> {
constructor(subject: TSubject)
on(value: TSubject, handler: Handler<TResult>): this
onAny(values: readonly TSubject[], handler: Handler<TResult>): this
otherwise(handler: Handler<TResult>): TResult
default(handler: Handler<TResult>): TResult
valueOf(): TResult
}See Matcher Class for details.
UnhandledMatchError
Error thrown when no case matches and no default is provided.
class UnhandledMatchError extends Error {
constructor(value: unknown)
name: 'UnhandledMatchError'
message: string
}Properties:
name- Always "UnhandledMatchError"message- Contains the unmatched value
Example:
import { match, UnhandledMatchError } from '@anilkumarthakur/match'
try {
match('foo')
.on('bar', () => 'not matched')
.valueOf()
} catch (error) {
if (error instanceof UnhandledMatchError) {
console.error('No match found:', error.message)
}
}Functions
match<TSubject, TResult>(subject: TSubject): Matcher<TSubject, TResult>
Creates a new match expression.
import { match } from '@anilkumarthakur/match'
const matcher = match(value)Type Parameters:
TSubject- The type of the value being matched (inferred from argument)TResult- The return type of handlers (inferred from usage)
Parameters:
subject- The value to match against
Returns: Matcher<TSubject, TResult> instance
See match() Function for details and examples.
Import Examples
// Named imports
import {
match,
Matcher,
UnhandledMatchError,
Handler,
MatchChain,
MatcherHandler
} from '@anilkumarthakur/match'
// Type-only imports (TypeScript)
import type { Handler, MatchChain, MatcherHandler } from '@anilkumarthakur/match'
// Mixed imports
import { match, type Handler } from '@anilkumarthakur/match'Type Safety
All types are fully exported for type-safe usage in TypeScript:
// Explicit type parameters
const matcher = match<string, number>('42')
.on('42', () => 42)
.otherwise(() => 0)
// Handler type
const handler: Handler<string> = () => 'result'
// MatchChain type
const chain: MatchChain<string, number> = match<string, number>('test').on('test', () => 123)Related
- Type Safety Guide - How to use types effectively
- Matcher Class - Detailed class documentation
- match() Function - Detailed function documentation