If you declared the return as an interface and returned a value other than the one defined. You will have an error
classUser { name:string;}constresult=Result.ok<User>("this is not user"); // Error
Result also accpet statusCode optionally, if you want to provide domain status for infra.
Result.ok<string>("Some success message","OK");// Available status successCONTINUE=100,SWITCHING_PROTOCOL=101,PROCESSING=102,OK=200,CREATED=201,ACCEPTED=202,NON_AUTHORITATIVE=203,NO_CONTENT=204,RESET_CONTENT=205,PARTIAL_CONTENT=206,
By default
success result has status "OK" = 200
failure result has "UNPROCESSABLE_ENTITY" = 422
You also can return a void using Result.success instead Result.ok. Let's see a real use case
constsaveUser=async (user:User):Promise<Result<void>> => {try{awaitconnection.save(user);// for void use Result.successreturnResult.success<void>(); }catch(error){returnResult.fail<void>(error.message,"INTERNAL_SERVER_ERROR"); }}// get resultconstresult=>awaitsaveUser(user);console.log(result.isSuccess);>true
It is possible to define an internationalization error message