eslint-plugin-total-functions

An ESLint plugin to enforce the use of TypeScript total functions.

Usage no npm install needed!

<script type="module">
  import eslintPluginTotalFunctions from 'https://cdn.skypack.dev/eslint-plugin-total-functions';
</script>

README

TypeScript Total Functions - ESLint Plugin

Build Status Type Coverage Test Coverage Mutation testing badge Known Vulnerabilities Total alerts Language grade: JavaScript Codacy Badge deepcode npm

dependencies Status devDependencies Status peerDependencies Status

An ESLint plugin to enforce the use of total functions (and prevent the use of partial functions) in TypeScript. If you like your types to tell the truth, this is the ESLint plugin for you.

Version Matrix

TypeScript ESLint eslint-plugin-total-functions Suppported?
4.5.4 8.5.0 5.0.0
4.4.2 7.32.0 4.10.1 No
4.3.5 7.30.0 4.8.0 No
4.1.2 7.12.0 4.7.2 No
4.0.2 7.9.0 3.3.0 No

Installation

yarn add --dev eslint-plugin-total-functions \
  @typescript-eslint/parser \
  eslint \
  typescript

Setup

Option 1

Use eslint-config-typed-fp which includes this plugin among others.

Option 2

  1. Turn on TypeScript's strict mode and noUncheckedIndexedAccess option.
  2. Set up ESLint + TypeScript.
  3. Turn on eslint-plugin-functional (recommended). Its rules related to mutation and OO are more important than this plugin's rules and they'll help keep your types honest.
  4. Update your .eslintrc.js:
module.exports = {
  parser: "@typescript-eslint/parser",
  parserOptions: {
    project: "./tsconfig.json",
    ecmaVersion: 2018,
    sourceType: "module"
  },
  extends: [
+    "plugin:total-functions/recommended",
  ],
  plugins: [
+    "total-functions",
  ],
};

Alternatively you can configure individual rules separately (see below).

Rules

Rule Recommended All Fixer?
require-strict-mode
no-unsafe-type-assertion
no-unsafe-readonly-mutable-assignment
no-unsafe-mutable-readonly-assignment Not yet
no-unsafe-optional-property-assignment Not yet

total-functions/require-strict-mode

The world is a very strange place when strict mode is disabled. This rule enforces strict mode and noUncheckedIndexedAccess mode (which is sadly not included under the strict umbrella).

total-functions/no-unsafe-type-assertion

Bans unsafe type assertions, for example:

type Foo = { readonly bar: number };
const foo = {} as Foo; // This compiles
foo.bar.toString(); // This explodes at runtime

This is similar to the consistent-type-assertions rule from typescript-eslint, however:

  1. this rule is weaker than consistent-type-assertions with its assertionStyle option set to never -- this rule will permit type assertions that it considers safe as opposed to blanket banning all type assertions, and
  2. this rule is stronger than consistent-type-assertions with its objectLiteralTypeAssertions option set to never, for example:
type Foo = { readonly bar: number };
const foo = {};
const foo2 = foo as Foo; // Flagged by this rule, but not by consistent-type-assertions (unless you set assertionStyle to never)
foo2.bar.toString(); // This explodes at runtime

For examples of type assertions that this rule considers valid and invalid, see no-unsafe-type-assertion.test.ts.

See TypeScript issue #7481 for a request to fix this at the language level.

total-functions/no-unsafe-readonly-mutable-assignment

Bans unsafe assignment of readonly values to mutable values (which can lead to surprising mutation in the readonly value). This includes passing readonly values as arguments to functions that expect mutable parameters.

For examples of assignment that this rule considers valid and invalid, see no-unsafe-readonly-mutable-assignment.test.ts.

See TypeScript issue #13347 for a request to fix this at the language level.

total-functions/no-unsafe-mutable-readonly-assignment

The inverse counterpart to no-unsafe-readonly-mutable-assignment. This rule bans unsafe assignment of mutable values to readonly values (which just like the inverse can lead to surprising mutation in the readonly value).

This rule is often noisy in practice so, unlike no-unsafe-readonly-mutable-assignment, is excluded from the recommended config.

Note that the following is considered an assignment from mutable to readonly:

  type ReadonlyA = { readonly a: string };
  const readonlyA: ReadonlyA = { a: "" };

The solution is to append as const to the RHS:

  type ReadonlyA = { readonly a: string };
  const readonlyA: ReadonlyA = { a: "" } as const;

For examples of assignment that this rule considers valid and invalid, see no-unsafe-mutable-readonly-assignment.test.ts.

total-functions/no-unsafe-optional-property-assignment

Optional properties (those with a ? after their name) interact badly with TypeScript's structural type system in a way that can lead to unsoundness. Example:

type Foo = { readonly foo: string };
type Bar = Foo & { readonly bar?: () => unknown };

const thing = { foo: "foo", bar: "bar" };
const foo: Foo = thing;
const bar: Bar = foo;

if (bar.bar !== undefined) {
    bar.bar(); // explodes at runtime
}

I find this scenario particularly vexing because it doesn't require type assertions, or plain JS with incorrect *.d.ts typings, or anything 'loose' like that. You can pull it off with otherwise nicely typed, functional TypeScript (strict mode enabled, no interfaces, no classes, everything readonly, everything const, no type assertions, no plain JS, etc).

This rule bans assignment from one type to another, if:

  1. the destination type has an optional property, and
  2. the source type has no matching property (either optional or otherwise).

This rule is excluded from the recommended config until #83 lands.

See Also