Skip to content

Commit

Permalink
Update README: How to use
Browse files Browse the repository at this point in the history
  • Loading branch information
yajamon committed Jan 26, 2020
1 parent cb73853 commit 2efe123
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,34 @@ Type definitions and simple functions inspired by Rust's Result.
```console
npm i -D @yajamon/result.ts
```

## Usage

```ts
import { Result, Ok, Err } from "@yajamon/result.ts"

class Foo {
static create(input:string): Result<Foo, Error> {
if (validation(input)) {
const foo = new Foo();
// ...
return Ok(foo);
} else {
return Err(new Error("error message"));
}
}

// ...
}

function bar(input:string) {
let foo = Foo.create(input);
if (foo.isError === true) {
// foo is ResultErr<Error>
console.log(foo.error);
return;
}
// foo is ResultOk<Foo>
console.log(foo.value);
}
```

0 comments on commit 2efe123

Please sign in to comment.