@@ -41,6 +41,7 @@ For asynchronous tasks, `neverthrow` offers a `ResultAsync` class which wraps a
4141 - [ ` Result.fromThrowable ` (static class method)] ( #resultfromthrowable-static-class-method )
4242 - [ ` Result.combine ` (static class method)] ( #resultcombine-static-class-method )
4343 - [ ` Result.combineWithAllErrors ` (static class method)] ( #resultcombinewithallerrors-static-class-method )
44+ - [ ` Result.struct ` (static class method)] ( #resultstruct-static-class-method )
4445 - [ ` Result.safeUnwrap() ` ] ( #resultsafeunwrap )
4546 + [ Asynchronous API (` ResultAsync ` )] ( #asynchronous-api-resultasync )
4647 - [ ` okAsync ` ] ( #okasync )
@@ -58,6 +59,7 @@ For asynchronous tasks, `neverthrow` offers a `ResultAsync` class which wraps a
5859 - [ ` ResultAsync.andThrough ` (method)] ( #resultasyncandthrough-method )
5960 - [ ` ResultAsync.combine ` (static class method)] ( #resultasynccombine-static-class-method )
6061 - [ ` ResultAsync.combineWithAllErrors ` (static class method)] ( #resultasynccombinewithallerrors-static-class-method )
62+ - [ ` ResultAsync.struct ` (static class method)] ( #resultasyncstruct-static-class-method )
6163 - [ ` ResultAsync.safeUnwrap() ` ] ( #resultasyncsafeunwrap )
6264 + [ Utilities] ( #utilities )
6365 - [ ` fromThrowable ` ] ( #fromthrowable )
@@ -800,6 +802,59 @@ const result = Result.combineWithAllErrors(resultList)
800802
801803[⬆️ Back to top ](#toc )
802804
805+ -- -
806+
807+ #### ` Result.struct ` (static class method )
808+
809+ > Although Result is not an actual JS class , the way that `struct ` has been implemented requires that you call `struct ` as though it were a static method on `Result `. See examples below .
810+
811+ Combine objects of `Result `s .
812+
813+ **`struct ` works on both heterogeneous and homogeneous objects **. This means that you can have objects that contain different kinds of `Result `s and still be able to combine them . Note that you cannot combine objects that contain both `Result `s **and ** `ResultAsync `s .
814+
815+ The `struct ` function takes an object of results and returns a single result . If all the results in the object are `Ok `, then the return value will be a `Ok ` containing an object of all the individual `Ok ` values .
816+
817+ If multiple results in the object are `Err ` then the `struct ` function returns an `Err ` containing an array of all the error values .
818+
819+ Example :
820+ ```typescript
821+ const resultObject : {
822+ a: Result <number , never >
823+ b: Result <number , never >
824+ } = {
825+ a: ok (1 ),
826+ b: ok (2 ),
827+ }
828+
829+ const combinedList : Result <{
830+ a: number
831+ b: number
832+ }, never > = Result .struct (resultObject )
833+ ` ` `
834+
835+ Example of error:
836+ ` ` ` typescript
837+ const resultObject : {
838+ a: Result < number , unknown>
839+ b: Result < never , number>
840+ c: Result < never , string>
841+ } = {
842+ a: ok (1 ),
843+ b: err (2 ),
844+ c: err (' 3' ),
845+ }
846+
847+ const combinedList : Result <{
848+ a: number
849+ b: unknown
850+ c: unknown
851+ }, (number | number )[]> = Result .struct (resultObject )
852+ ` ` `
853+
854+ [⬆️ Back to top](#toc)
855+
856+ ---
857+
803858#### ` Result .safeUnwrap ()`
804859
805860**Deprecated**. You don't need to use this method anymore.
@@ -1410,6 +1465,57 @@ const result = ResultAsync.combineWithAllErrors(resultList)
14101465// result is Err(['boooom!', 'ahhhhh!'])
14111466` ` `
14121467
1468+ -- -
1469+
1470+ #### ` ResultAsync.struct ` (static class method )
1471+
1472+ Combine objects of `ResultAsyncs `s .
1473+
1474+ **`struct ` works on both heterogeneous and homogeneous objects **. This means that you can have objects that contain different kinds of `Result `s and still be able to combine them . Note that unlike `Result .struct `, you can combine objects containing both `Result `s and `ResultAsync `s .
1475+
1476+ The `struct ` function takes an object of results and returns a single result . If all the results in the object are `Ok `, then the return value will be a `Ok ` containing an object of all the individual `Ok ` values .
1477+
1478+ If multiple results in the object are `Err ` then the `struct ` function returns an `Err ` containing an array of all the error values .
1479+
1480+ Example :
1481+ ```typescript
1482+ const resultObject : {
1483+ a: ResultAsync <number , never >
1484+ b: ResultAsync <number , never >
1485+ } = {
1486+ a: okAsync (1 ),
1487+ b: okAsync (2 ),
1488+ }
1489+
1490+ const combinedList : ResultAsync <{
1491+ a: number
1492+ b: number
1493+ }, never > = ResultAsync .struct (resultObject )
1494+ ` ` `
1495+
1496+ Example of error:
1497+ ` ` ` typescript
1498+ const resultObject : {
1499+ a: ResultAsync < number , unknown>
1500+ b: ResultAsync < never , number>
1501+ c: ResultAsync < never , string>
1502+ } = {
1503+ a: okAsync (1 ),
1504+ b: errAsync (2 ),
1505+ c: errAsync (' 3' ),
1506+ }
1507+
1508+ const combinedList : ResultAsync <{
1509+ a: number
1510+ b: unknown
1511+ c: unknown
1512+ }, (number | number )[]> = ResultAsync .struct (resultObject )
1513+ ` ` `
1514+
1515+ [⬆️ Back to top](#toc)
1516+
1517+ ---
1518+
14131519#### ` ResultAsync .safeUnwrap ()`
14141520
14151521**Deprecated**. You don't need to use this method anymore.
0 commit comments