背景 TypeScriptの教材で以下のサンプルコードがあったので、コードリーディングしていく。
概要 以下のコードリーディングをしていく。
非同期データ取得関数をラップして、そこで起きる例外をフォローしつつ正常系と異常系に振り分ける関数を定義している。
type Result<T, E extends Error> = Ok<T, E> | Err<T, E> export class Ok<T, E extends Error> { constructor(readonly val: T) {} isOk = (): this is Ok<T, E> => true isErr = (): this is Err<T, E> => false } export class Err<T, E extends Error> { constructor(readonly err: E) {} isOk = (): this is Ok<T, E> => false isErr = (): this is Err<T, E> => true } export const withResult = <T, A extends any[], E extends Error>(fn: (.