# Result
Error handling with the Result type.

`Result<T, E>` is the type used for returning and propagating errors. It is an union type with the variants, `Ok<T>`, representing success and containing a value, and `Err(E)`, representing error and containing an error value.

As such the way to use this type is as follows:
```typescript
import { Result, Ok, Err, isOk, isErr } from 'Result';

// Lets define a success type and an error type
interface SomeData {
    thing: string,
    stuff: boolean,
}

enum SomeError {
    NoFunIsBeingHad,
    WeDoWhatWeDoBecauseWeCan,
    MaybeOtherStuff,
}

// Now we can define our own Result wrapper if we don't want to specify types all the time
type SomeResult = Result<SomeData, SomeError>;

// Or if either one changes per function, maybe we should only typedef one side
type CustomSuccessResult<T> = Result<T, SomeError>;
type CustomErrorResult<E> = Result<SomeData, E>;

// Or we can just use it by itself in a function

// Perhaps we return a result
function test(): Result<number, string> {
    if (random() > 0.5) {
        return Ok(42);
    } else {
        return Err("Sorry, this is going to take a while");
    }
}

// Or we use it
function inspect(result: Result<SomeData, SomeError>) {
    // If we care about the Successful result
    if (isOk(result)) {
        const okValue = result.value;
        console.log("We've got a succesful value of ", okValue.thing, " being: ", okValue.stuff);
    }

    // If we only care about the error
    if (isErr(result)) {
        const errValue = result.value;
        console.log("We've got an error value of ", errValue);
    }

    // But typescript is smart so it is up to you which one you use as the following is valid as well
    if (isOk(result)) {
        const okValue = result.value;
        console.log("We've got a succesful value of ", okValue.thing, " being: ", okValue.stuff);
    } else {
        const errValue = result.value;
        console.log("We've got an error value of ", errValue);
    }

    // Or this
    if (isErr(result)) {
        const errValue = result.value;
        console.log("We've got an error value of ", errValue);
    } else {
        const okValue = result.value;
        console.log("We've got a succesful value of ", okValue.thing, " being: ", okValue.stuff);
    }
}
```

**_Note_**: If TS complains about types while using `Ok` or `Err` you can do the following `Ok<T, E>(value)` with `T` and `E` having the correct values, similarly with `Err`.
