File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -1292,3 +1292,33 @@ export function joinAtMost(
12921292
12931293 return array . join ( separator ) ;
12941294}
1295+
1296+ type Success < T > = Result < T , never > ;
1297+ type Failure < E > = Result < never , E > ;
1298+
1299+ export class Result < T , E > {
1300+ private constructor (
1301+ private readonly _ok : boolean ,
1302+ public readonly value : T | E ,
1303+ ) { }
1304+
1305+ static ok < T > ( value : T ) : Success < T > {
1306+ return new Result ( true , value ) as Success < T > ;
1307+ }
1308+
1309+ static error < E > ( error : E ) : Failure < E > {
1310+ return new Result ( false , error ) as Failure < E > ;
1311+ }
1312+
1313+ isOk ( ) : this is Success < T > {
1314+ return this . _ok ;
1315+ }
1316+
1317+ isError ( ) : this is Failure < E > {
1318+ return ! this . _ok ;
1319+ }
1320+
1321+ orElse ( defaultValue : T ) : T {
1322+ return this . _ok ? ( this . value as T ) : defaultValue ;
1323+ }
1324+ }
You can’t perform that action at this time.
0 commit comments