Add native-TS functional toolkit (assertNever, Result, Brand)
The shared foundation for the "make impossible states impossible" work: - assertNever for compile-time exhaustiveness in union switches - Result<E,T> + ok/err constructors (plain objects, no classes) - Brand<T,B> for nominal types No runtime dependency — this is the whole "library". Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
23
src/app/core/fp.ts
Normal file
23
src/app/core/fp.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Tiny native-TS functional toolkit. No dependency — this is the whole "library".
|
||||
* Reused by every "impossible states" concept in the POC.
|
||||
*/
|
||||
|
||||
/** Exhaustiveness guard: put in the `default` arm of a union switch. Adding a
|
||||
new variant without handling it then fails to compile (x is no longer never). */
|
||||
export function assertNever(x: never): never {
|
||||
throw new Error('Unexpected variant: ' + JSON.stringify(x));
|
||||
}
|
||||
|
||||
/** A computation that either succeeded with a value or failed with an error.
|
||||
Plain objects (no classes) to match the signal/httpResource ergonomics. */
|
||||
export type Result<E, T> =
|
||||
| { readonly ok: true; readonly value: T }
|
||||
| { readonly ok: false; readonly error: E };
|
||||
|
||||
export const ok = <T>(value: T): Result<never, T> => ({ ok: true, value });
|
||||
export const err = <E>(error: E): Result<E, never> => ({ ok: false, error });
|
||||
|
||||
/** Nominal typing: Brand<string, 'Postcode'> is assignable from a plain string
|
||||
only through an explicit cast — so a smart constructor is the only minter. */
|
||||
export type Brand<T, B extends string> = T & { readonly __brand: B };
|
||||
Reference in New Issue
Block a user