neverthrow icon indicating copy to clipboard operation
neverthrow copied to clipboard

combineWithAllErrors doesn't narrow down type of items in list

Open Sebastian-Nielsen opened this issue 2 years ago • 1 comments

	if (combineWithAllErrors([ result1, result2, result3 ]).isErr()) {
	        console.log("some error")
		return err();
	}
	
	// We know for sure know that result1, result2, and result3 are ok:
	result1.value; // TS2339: property 'value' does not exist on type 'Result' 
	
	// ^^ it seems typescript doesn't know?

I am forced to do:

	if (combineWithAllErrors([ result1, result2, result3 ]).isErr()) {
	        console.log("some error")
		return err();
	}
	
	if (result1.isErr()) {    
		return 1 as any;
	}
        console.log(result1.value); // works

Can we do something about the fact that combineWithAllErrors doesn't narrow down the type for the items in the provided list?

Sebastian-Nielsen avatar Nov 28 '23 15:11 Sebastian-Nielsen

combineWithAllErrors generates a new Result and you have to handle it. It's not supposed to narrow down the type.

If you want to write something like that, you need to add another utility function

m-shaka avatar Sep 06 '24 00:09 m-shaka