strict-ts strict-ts

Examples

transfer

Ownership: a transfer whose transaction may be left open or used twice, against a version where it has one owner.

These are the sources of the example, as they are at the commit this site was built from: classic is what tsc --strict accepts, strict what conforms. Examples says what each is refused for, and each is a scenario of the playground, where it can be changed.

#classic/strict-ts.json

examples/transfer/classic/strict-ts.json
{}

#classic/transfer.ts

examples/transfer/classic/transfer.ts
// "Classic" TypeScript: a transaction is a value like any other, and nothing says it must be ended, once.
export class Transaction {
  private open = true;
  private readonly entries: string[] = [];

  debit(account: string, cents: number): void {
    this.entries.push(`${account} -${cents}`);
  }
  credit(account: string, cents: number): void {
    this.entries.push(`${account} +${cents}`);
  }
  commit(): string[] {
    if (!this.open) throw new Error("already ended");
    this.open = false;
    return this.entries;
  }
  rollback(): void {
    if (!this.open) throw new Error("already ended");
    this.open = false;
  }
}

const audit: Transaction[] = [];

export function transfer(from: string, to: string, cents: number): string[] {
  const tx = new Transaction();
  tx.debit(from, cents);
  // Left open: this path neither commits nor rolls back.
  if (cents <= 0) return [];
  tx.credit(to, cents);
  // A second owner: whoever reads `audit` can end it too.
  audit.push(tx);
  const written = tx.commit();
  // Used after it was ended: throws at run time.
  tx.rollback();
  return written;
}

#strict/strict-ts.json

examples/transfer/strict/strict-ts.json
{
  "unsafeDirs": ["unsafe"]
}

#strict/transfer.ts

examples/transfer/strict/transfer.ts
// The same transfer, conforming: a transaction has one owner, and is ended exactly once.
import { end, err, ok, type Mut, type Once, type Result } from "@strict-ts/std";

export class Transaction {
  private readonly entries: Mut<string[]> = [];

  debit(this: Mut<Transaction>, account: string, cents: number): void {
    this.entries.push(`${account} -${cents}`);
  }
  credit(this: Mut<Transaction>, account: string, cents: number): void {
    this.entries.push(`${account} +${cents}`);
  }
  written(): readonly string[] {
    return this.entries;
  }
}

/** The one way to get a transaction: it comes with its duty. */
export const begin = (): Once<Mut<Transaction>> => new Transaction();

/** Takes the transaction for good. Nobody can use it after. */
export const commit = (tx: Once<Transaction>): readonly string[] => end(tx).written();
export const rollback = (tx: Once<Transaction>): void => {
  end(tx);
};

export function transfer(from: string, to: string, cents: number): Result<readonly string[], "nothing-to-move"> {
  const tx = begin();
  tx.debit(from, cents);
  if (cents <= 0) {
    // Without this line: « `tx` is not given away before this `return` ».
    rollback(tx);
    return err("nothing-to-move");
  }
  tx.credit(to, cents);
  // After this line, any use of `tx` is refused: « used after it was given away ».
  return ok(commit(tx));
}
Esc

    Type a rule, a word of the library, a command: no-cast, Result, baseline.