strict-ts strict-ts

The rules

The rules

#The rules

« Scope » says where a rule runs: safe is everything outside the unsafe directories, unsafe is inside them, all is both, project is once for the whole project.

RuleGuaranteeScopeWhat it asks
no-anytypingsafeNo any, written or inferred (JSON.parse for instance)
no-casttypingsafeNo type assertion (as T, <T>x). as const and satisfies stay allowed
no-non-nulltypingsafeNo non-null assertion x!, no definite assignment x!: T
no-type-predicatetypingsafeNo written type predicate (x is T, asserts x is T). The ones the compiler infers stay
no-overloadtypingsafeNo overload signature: nothing checks that the implementation keeps each promise, and one can be a cast
no-method-signaturetypingsafeNo method syntax in an interface or a type (name(x: T): R): written name: (x: T) => R, it is compared soundly
no-numeric-enumtypingsafeNo enum with a member that is a number: any number is accepted where one is asked for. An enum of texts stays
no-default-on-undefinedtypingsafeA default is not written where the type lets undefined be given on purpose: in a parameter, in a pattern, in an assignment by a pattern. And a function with a default is not given where one that takes undefined on purpose is asked for
no-unset-capturetypingsafeA variable declared with no value (let ready: string;) is not used by a function: nothing shows it has a value when the function runs
no-early-readerrorssafeA value is not read before its line: no function is used at the top of a file above a variable it reads, and files do not import each other in a circle
no-rejecterrorssafeA promise is not made to fail: the callback of new Promise does not take the function that rejects it, and no object has a then of its own
no-stale-narrowingtypingsafeWhat a test narrowed is not relied on after something else could change it: a call or an await before the read of a variable another function writes or of a property that can be written, and any read of a getter. A read that is itself a test (a comparison, a value read for its truth), or that is given where the whole declared type is taken, relies on nothing and is accepted. Not run in test files
no-class-lookaliketypingsafeWhere a class is asked for, the value is made by that class, not an object of its shape
no-loose-spreadtypingsafeA spread does not write a property its type does not name: over one written before it, or into one the result may have
no-in-narrowingtypingsafein does not choose between the members of a union: a tag does
no-open-readtypingsafeObject.values and Object.entries read a table (Record) only, and a table is not made from a value with named properties: it may hold more than its type names
no-keyed-writetypingsafeA property is written through a key only when the key is one name
no-resizetypingsafeAn array gets no hole and a tuple keeps its length: no assignment to length, no write by position (splice(i, 1, v)), no push or pop on a tuple
mut-exacttypingsafeWhat is given where Mut<…> is asked for has exactly that type: a Mut<string[]> is not a Mut<(string | number)[]>
no-unbound-methodtypingsafeA method is called through its object: taken off it as a value it loses this
no-early-thistypingsafeIn a constructor or in the value of a property, no method of this is called and this is not handed over: the object is not complete
no-ambienttypingsafeNo declare, and no declaration file of the project outside an unsafe directory
no-external-importtypingsafeDependencies (npm, node:*) are imported from an unsafe directory only
no-ts-directivetypingallNo @ts-ignore, @ts-expect-error or @ts-nocheck, unsafe directories included
unsafe-boundarytypingunsafeThe functions an unsafe directory exports return Result or Option, and leak no any
safety-commenttypingunsafeAn any, a cast, a !, a written predicate or a declare has // SAFETY: <reason> right above
strict-compiler-optionstypingprojectstrict, noUncheckedIndexedAccess, exactOptionalPropertyTypes, noImplicitReturns, noFallthroughCasesInSwitch, noImplicitOverride
unread-sourcestypingprojectA source the gate does not read (.vue, .svelte, .astro) is refused as a whole: nothing in it is checked
no-throwerrorssafeNo throw: errors are values (Result)
no-nullerrorssafeNo null, as a value or as a type. Comparing with === null stays allowed
must-useerrorsallA Result, an Option or a Promise is not ignored. To drop one on purpose: void expr
exhaustive-switcherrorsallA switch covers every member of a finite union, or has a default
no-throwing-callerrorsallA call that may throw without its type saying so (a library function the effects ledger does not show to be safe, any function of a package that is not allowed) is made inside attempt, or in a try with a catch
no-throwing-operatorerrorsallAn operator or a read that may throw on what its operands may be (a bigint /, % or **, the conversion of a value that is not plain in a template, a + or a comparison, an array length that is not a literal, a property of the library that runs code) is written in a try with a catch
freeze-fresherrorsallObject.freeze, seal and preventExtensions are given a value where it is made (a literal, a new), so that no name still says it can be changed
use-onceownershipallA value typed Once<…> has one owner: it is used through its members, given away exactly once (to a parameter or a return type that is Once<…>), and never lost. Giving it away in a loop, a try, a condition inside an expression or a closure is refused
no-param-mutationmutabilitysafeA function changes only what it made, or what says it may be changed. Not a parameter unless it is typed Mut<T>: by its name, by another name for it or for a part of it, by what a method of a list hands out of it (list.slice()[0], new Map(map)), by a function of the library that may hand back what it was given, by a function of the platform, by push.call(…). Not the object a method is called on unless the method says this: Mut<…>, Rust's &mut self, under the name this or another. Not what a function of the project gave back unless its return type says Mut<…>, and not what the list it gave back holds unless the return type says it inside. And not what a field or a list holds unless the field says Mut<…>, or the list says it inside: of its values for a map, of each element for a tuple. Nor what another module made and exports without Mut<…>
mut-from-readonlymutabilitysafeWhat is given where Mut<…> is asked for is the giver's to change, asked the way a write is: not a parameter that does not ask for mutation, not this or a part of it in a method that does not say this: Mut<…>, not what a function gave back without saying so, under any name; not a value typed read-only. As an argument, a returned value, a property or an element of what is written in place, the right side of an assignment, what is pushed into a list whose type says Mut<…> inside, the receiver of a method that says this: Mut<…>. A new list or object of the same things is the giver's own; where Mut<…> is asked of what it holds, the things have to be. A function that writes its parameter is not given where one that does not is asked for, and is given to a function of the library only with what the giver may change. A method does not say this: Mut<…>, or ask it of a parameter, where the member it stands for in a class it extends or an interface it implements does not; nor does an object given under a type whose members say less. call, apply, bind and a spread list give arguments as a call does
readonly-typesmutabilitysafeA written type is read-only (readonly T[], ReadonlyMap, readonly properties) unless under Mut<…>
pure-domainmutabilitysafeIn a directory listed in pureDirs: no clock, randomness, console, network or module-level state, and no import from outside the pure directories

The compiler's own errors are reported too, as TS2322 and the like. There is no comment that silences a rule, by design: what cannot conform goes to an unsafe directory with its reason, or to the baseline.

#Ownership: a value with one owner

A transaction that is neither committed nor rolled back, a lock released twice, a handle used after it was closed: all compile. Once<T> marks a value that has one owner and is given away exactly once, as a value Rust moves.

import { end, type Once } from '@strict-ts/std';

export function begin(): Once<Transaction> { … }
export function commit(tx: Once<Transaction>): Result<void, DbError> { const raw = end(tx); … }

const tx = begin();
tx.query('…');                        // used through its members: fine
if (failed) return err('failed');     // refused: `tx` is not given away before this `return`
commit(tx);
tx.query('…');                        // refused: `tx` is used after it was given away

The rule use-once accepts a small subset, which it can prove: a value given away in plain sequence, or in both branches of an if. In a loop, a try, a condition inside an expression or a closure, it refuses rather than guess.

Esc

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