@@ -1291,42 +1291,51 @@ export function joinAtMost(
12911291 return array . join ( separator ) ;
12921292}
12931293
1294- /** A success result. */
1295- type Success < T > = Result < T , never > ;
1296- /** A failure result. */
1297- type Failure < E > = Result < never , E > ;
1294+ /** An interface representing something that is either a success or a failure. */
1295+ interface ResultLike < T , E > {
1296+ /** The value of the result, which can be either a success value or a failure value. */
1297+ value : T | E ;
1298+ /** Whether this result represents a success. */
1299+ isSuccess ( ) : this is Success < T > ;
1300+ /** Whether this result represents a failure. */
1301+ isFailure ( ) : this is Failure < E > ;
1302+ /** Get the value if this is a success, or return the default value if this is a failure. */
1303+ orElse < U > ( defaultValue : U ) : T | U ;
1304+ }
12981305
1299- /**
1300- * A simple result type representing either a success or a failure.
1301- */
1302- export class Result < T , E > {
1303- private constructor (
1304- private readonly _ok : boolean ,
1305- public readonly value : T | E ,
1306- ) { }
1307-
1308- /** Creates a success result. */
1309- static success < T > ( value : T ) : Success < T > {
1310- return new Result ( true , value ) as Success < T > ;
1306+ /** A simple result type representing either a success or a failure. */
1307+ export type Result < T , E > = Success < T > | Failure < E > ;
1308+
1309+ /** A result representing a success. */
1310+ export class Success < T > implements ResultLike < T , never > {
1311+ constructor ( public readonly value : T ) { }
1312+
1313+ isSuccess ( ) : this is Success < T > {
1314+ return true ;
13111315 }
13121316
1313- /** Creates a failure result. */
1314- static failure < E > ( value : E ) : Failure < E > {
1315- return new Result ( false , value ) as Failure < E > ;
1317+ isFailure ( ) : this is Failure < never > {
1318+ return false ;
13161319 }
13171320
1318- /** Whether this result represents a success. */
1319- isSuccess ( ) : this is Success < T > {
1320- return this . _ok ;
1321+ orElse < U > ( _defaultValue : U ) : T {
1322+ return this . value ;
1323+ }
1324+ }
1325+
1326+ /** A result representing a failure. */
1327+ export class Failure < E > implements ResultLike < never , E > {
1328+ constructor ( public readonly value : E ) { }
1329+
1330+ isSuccess ( ) : this is Success < never > {
1331+ return false ;
13211332 }
13221333
1323- /** Whether this result represents a failure. */
13241334 isFailure ( ) : this is Failure < E > {
1325- return ! this . _ok ;
1335+ return true ;
13261336 }
13271337
1328- /** Get the value if this is a success, or return the default value if this is a failure. */
1329- orElse < U > ( defaultValue : U ) : T | U {
1330- return this . isSuccess ( ) ? this . value : defaultValue ;
1338+ orElse < U > ( defaultValue : U ) : U {
1339+ return defaultValue ;
13311340 }
13321341}
0 commit comments