Skip to content

chris-muller/ts-result-type

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ts-result-type

This contains the types and helper methods for a typescript result type that is clear and easy to use.

A lot of the logic is inspired (or copy/pasted) from the following:

Examples

function returnsSomeResult(foo?: boolean): Result<string, number> {
	if (foo) {
		// Wraps the value in the success result type
		return success("Success string");
	}

	// Wraps the value in the failure result type
	return failure(404);
}

function someMethod() {
	const result = returnsSomeResult(true);

	// Inside this if statement we know `result.failure` is a number
	// and `result.success` is undefined
	if (result.isFailure()) {
		const test1 = result.failure;

		// `unwrap` returns the failure value and typescript knows `testunwrap` is a number
		const testunwrap = result.unwrap();
	}

	// typescript narrows type inside isSuccess helper
	if (result.isSuccess()) {
		const test2 = result.success;
		const test3 = result.failure;
		const outcome = result.outcome;

		// `unwrap` returns the success value and typescript knows `testunwrap` is a string
		const testunwrap = result.unwrap();
	}

	// returns the success or failure value and typescript only
	// knows that testunwrap is either a number or string
	const testunwrap = result.unwrap();
}

Reasoning

  • This uses Result over Either as this type is likely to be used only for the result of an operation 99% of the time. Either types also don't implicitly imply a success/fail state, just a value that can be one of two types of outcome.
  • Using the success and failure terms over left and right as this type implicitly communicates a success or failure state unlike Either as well as the 'convention' of using Left for failures seemed too non-obvious when looking through code examples (every article on the topic had to go out of its way to explain the convention), and it made the helper methods clearer when reading through the code.
  • Helpers like isSuccess or unwrap are available on the result value itself, allowing the result to be inspected wherever it's passed without needing to import the helper methods. Helper methods are available separately, however I'm not sure they're worth exporting outside of the module.
  • Unlike languages like Swift or Rust we can't pattern match against the type of the result itself, so the outcome property has been added as a convenience when using switch statements or other purposes.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published