An educational pure functional programming library in TypeScript
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Improve type safety in interruptFiber and document pattern matching casts

+18 -8
+18 -8
src/index.ts
··· 127 127 (value: Extract<T, { _tag: K }>) => R 128 128 } 129 129 130 - /** Exhaustive pattern matching on tagged unions */ 131 - export const match = <T extends { _tag: string }>(value: T) => 132 - <R>(patterns: Pattern<T, R>): R => 130 + /** 131 + * Exhaustive pattern matching on tagged unions. 132 + * 133 + * The `any` cast is safe because Pattern<T, R> requires handlers for all variants, 134 + * and we index by the actual _tag value which TypeScript has already narrowed. 135 + */ 136 + export const match = <T extends { _tag: string }>(value: T) => 137 + <R>(patterns: Pattern<T, R>): R => 133 138 patterns[value._tag as keyof typeof patterns](value as any) 134 139 135 - /** Match with wildcard fallback */ 136 - export const matchOr = <T extends { _tag: string }, R>(defaultValue: R) => 137 - (value: T) => 138 - (patterns: Partial<Pattern<T, R>>): R => 140 + /** 141 + * Match with wildcard fallback for partial pattern matching. 142 + * 143 + * The `any` cast is safe because we use optional chaining and fall back to defaultValue 144 + * when the handler is undefined. 145 + */ 146 + export const matchOr = <T extends { _tag: string }, R>(defaultValue: R) => 147 + (value: T) => 148 + (patterns: Partial<Pattern<T, R>>): R => 139 149 (patterns[value._tag as keyof typeof patterns] as any)?.(value) ?? defaultValue 140 150 141 151 /** Predicate-based matching */ ··· 552 562 ({ _tag: "Fork", effect }) as Eff<Fiber<A, E>, never, R> 553 563 554 564 /** Interrupt a fiber */ 555 - export const interruptFiber = (fiber: Fiber<any, any>): Eff<void, never, unknown> => 565 + export const interruptFiber = <A, E>(fiber: Fiber<A, E>): Eff<void, never, unknown> => 556 566 async((resume) => { 557 567 fiber.interrupt().then(() => resume(Exit.succeed(undefined))) 558 568 })