You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In #603, @pierback mentioned that only the first yield is considered in the safeTry type inference due to a TypeScript limitation (see microsoft/TypeScript#57625). This is a significant drawback of using safeTry and should be noted in the documentation, as it can easily lead to bugs due to incomplete error handling. I'll have to check my codebases now and see wether this is an issue in my code 😌
Perhaps neverthrow could include an example like this:
classMyError1extendsError{}classMyError2extendsError{}declarefunctionmayFail1(): Result<number,MyError1>declarefunctionmayFail2(): Result<number,MyError2>// Return type is Result<number | null, MyError1 | MyError2>functionmyFunc(){returnsafeTry<number|null,MyError1|MyError2>(function*(){constvalue1=yield*mayFail1()constvalue2=yield*mayFail2()constresult=value1+value2if(result<0){returnok(null)}returnok(result)})}// Inferred return type is Result<number | null, MyError1>functionmyInferredFunc(){returnsafeTry(function*(){constvalue1=yield*mayFail1()constvalue2=yield*mayFail2()constresult=value1+value2if(result<0){returnok(null)}returnok(result)})}
Including this in the safeTry documentation would illustrate both the issue and the expected inference of ok values. The documentation should also emphasize that when using safeTry, developers must always specify all potential errors as generics.
The text was updated successfully, but these errors were encountered:
FYI, you can use a tag (or really any property that differentiates the 2 types) to prevent this. I would suggest instead updating the documentation to recommend this approach.
In #603, @pierback mentioned that only the first
yield
is considered in thesafeTry
type inference due to a TypeScript limitation (see microsoft/TypeScript#57625). This is a significant drawback of usingsafeTry
and should be noted in the documentation, as it can easily lead to bugs due to incomplete error handling. I'll have to check my codebases now and see wether this is an issue in my code 😌Perhaps neverthrow could include an example like this:
Including this in the
safeTry
documentation would illustrate both the issue and the expected inference ofok
values. The documentation should also emphasize that when usingsafeTry
, developers must always specify all potential errors as generics.The text was updated successfully, but these errors were encountered: