Skip to content

0.0.2 Type System Improvements

Compare
Choose a tag to compare
@ybabts ybabts released this 17 Jun 02:52
· 33 commits to main since this release

Overview

This release changes a lot with how Typescript's type system interacts with the already existing functions. Much work has been done to make sure that Typescript knows when and what errors exist on a particular value and allow for proper narrowing with isErr. This also introduces a new concept of causality. Every error has a cause property on it that identifies it as an error of a particular type. This is compatible with numbers and strings, which makes using enums possible to both identify and reuse identifying causes for errors in a function.

Changelog:

  1. Linting Rule Change: The updated code now includes a directive to ignore the "no-explicit-any" linting rule for the entire file.

  2. Type Changes:

    • The Result type now includes CustomError as a possible error type.
    • The CustomError type has been expanded to include a cause property of type C which extends ValidCause, and a message property of type M which extends string.
  3. Function Changes:

    • The addMsg function now checks if the error message length is less than or equal to 0 before assigning the message.
    • The Err function now accepts a cause argument and an optional message argument, and returns a CustomError with these properties.
    • The Try function now accepts an optional cause argument and returns a CustomError with this cause if an error is thrown.
    • The isErr function now accepts an optional cause argument and checks if the value's cause matches this cause when determining if the value is an error.
    • The Ok function now returns Exclude<T, Error> instead of T, excluding Error from the possible return types.
  4. New Types and Functions:

    • A new type ValidCause has been introduced, which can be a string or number.
    • A new type ExtractCause has been introduced, which extracts the cause property from a given type.
    • A new function addMsg has been introduced, which adds a message to an error and returns a CustomError.

Example

enum DatabaseErrorCause {
  NotFound = 'NOT_FOUND',
  ConnectionError = 'CONNECTION_ERROR',
  UnexpectedError = 'UNEXPECTED_ERROR',
}

type DatabaseError = CustomError<DatabaseErrorCause, string>;

function fetchUserData(id: string): Result<UserData, DatabaseError> {
  // ... fetch user data from database
  // If an error occurs, return a CustomError with a specific cause
  if (error) {
    return Err(DatabaseErrorCause.NotFound, `User with id ${id} not found.`);
  }
  // If everything is fine, return the user data
  return userData;
}

const result = fetchUserData('123');

const errorMessages = {
  [DatabaseErrorCause.NotFound]: 'User not found. Please check the id.',
  [DatabaseErrorCause.ConnectionError]: 'Could not connect to the database. Please check your connection.',
  [DatabaseErrorCause.UnexpectedError]: 'An unexpected error occurred.',
};

if (isErr(result)) {
  console.log(errorMessages[result.cause]);
} else {
  console.log('User data:', result);
}

Full Changelog: 0.0.1...0.0.2