covariant-alias
A value whose written type is a container of one type (Sub[], Box<Sub>, Map<string, Sub>) bound to a place written with the same container of another, wider type (Base[]), where a value can be written into it: a local, a field, a return, a property of an object literal. On by default, as an error.
Why: TypeScript checks a generic's type arguments one way: an array of circles is an array of shapes. That holds for reading, not for writing. Through the wider alias a plain shape goes in, and it lands in the circles' array, whose readers call radius() on it and crash. Rust's &mut Vec<T> is invariant in T for this reason. A call is covered already: a Circle[] can't be lent as Mut<Shape[]>, and a parameter not declared Mut is only read. What is left is an alias the function keeps. Guide: State without globals.
✕ Rejectedcovariant-alias
ts
type Shape = { readonly kind: string };
type Circle = { readonly kind: string; readonly radius: () => number };
export function largest(): number {
const circles: Circle[] = [{ kind: "circle", radius: () => 2 }];
const shapes: Shape[] = circles;
shapes.push({ kind: "square" });
return circles.reduce((most, circle) => Math.max(most, circle.radius()), 0);
}✅ Accepted: the wider side can't be written, or it is a copy
✓ Accepted0 diagnostics
ts
type Shape = { readonly kind: string };
type Circle = { readonly kind: string; readonly radius: () => number };
export function count(circles: readonly Circle[]): number {
const shapes: readonly Shape[] = circles;
return shapes.length;
}
export function withSquare(circles: readonly Circle[]): Shape[] {
const shapes: Shape[] = [...circles];
shapes.push({ kind: "square" });
return shapes;
}The fix is readonly on the wider side (readonly T[], ReadonlyMap, ReadonlySet, Readonly<...>, readonly fields in your own generic type), or a copy ([...xs], new Map(m)).
A function property, lent
A value lent with mut(x) (or a Mut value passed on) to a parameter whose type has a writable property of function type that takes a narrower parameter, or returns a wider result, than the same property in the value's own written type: TypeScript checks a function property one way (a handler of animals is a handler of dogs), so the callee may install a function that needs a dog, and the owner's own calls still pass any animal.
✕ Rejectedcovariant-alias
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
type Animal = { readonly name: string };
type Dog = Animal & { readonly bark: () => string };
type Listener = { onEvent: (a: Animal) => string };
function install(target: Mut<{ onEvent: (d: Dog) => string }>): void {
target.onEvent = (d) => d.bark();
}
export function swap(listener: Mut<Listener>): string {
install(mut(listener));
return listener.onEvent({ name: "cat" });
}ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
type Animal = { readonly name: string };
type Dog = Animal & { readonly bark: () => string };
type Listener = { onEvent: (a: Animal) => string };
function install(target: Mut<{ onEvent: (d: Dog) => string }>): void {
target.onEvent = (d) => d.bark();
}
export function swap(listener: Mut<Listener>): string {
install(listener);
return listener.onEvent({ name: "cat" });
}✅ Accepted: the callee can't replace it, or both sides agree
✓ Accepted0 diagnostics
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
type Animal = { readonly name: string };
type Dog = Animal & { readonly bark: () => string };
type Listener = { onEvent: (a: Animal) => string; calls: number };
function count(target: Mut<{ readonly onEvent: (d: Dog) => string; calls: number }>): void {
target.calls += 1;
}
function install(target: Mut<{ onEvent: (a: Animal) => string }>): void {
target.onEvent = (a) => a.name;
}
export function swap(listener: Mut<Listener>): string {
count(mut(listener));
install(mut(listener));
return listener.onEvent({ name: "cat" });
}ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
type Animal = { readonly name: string };
type Dog = Animal & { readonly bark: () => string };
type Listener = { onEvent: (a: Animal) => string; calls: number };
function count(target: Mut<{ readonly onEvent: (d: Dog) => string; calls: number }>): void {
target.calls += 1;
}
function install(target: Mut<{ onEvent: (a: Animal) => string }>): void {
target.onEvent = (a) => a.name;
}
export function swap(listener: Mut<Listener>): string {
count(listener);
install(listener);
return listener.onEvent({ name: "cat" });
}Narrower, as the written types show: a type with every property of the other's and more (Dog for Animal, one level down too), a union of fewer named types, a type without undefined where the other has it (a result string | undefined where the value's returns string). The two types are read as written, across files, through aliases, interfaces, intersections and Mut<...>: the value's (a local's or parameter's annotation, a field's, a module-level const's) and the type the callee's Mut<...> parameter lends it as. A readonly property, a method written as a method and a class's method in the parameter's type aren't replaced through it; a value's member written as a method is compared like a property.
What it compares
The types written on both sides: the value's (a local's or parameter's annotation, x: Mut<T> as T, or a field's) and the place's (an annotated const or let, a local assigned later, a field assigned through this, the function's return type, a property of an object type the annotation writes or names). They must be the same container with different type arguments, as written after the file's type aliases are followed and tuple labels dropped. Arrays, Array<T>, Map, WeakMap, Set, WeakSet and Record can be written; so can a generic type of the file whose writable property, index signature or method parameter uses the type parameter, Vue's Ref and ShallowRef (imported from vue: .value is writable), and React's RefObject and MutableRefObject (from react: .current). readonly arrays, ReadonlyArray, ReadonlyMap, ReadonlySet and Readonly<...> can't. A generic type of a package the checker knows nothing about isn't reported.
Returning a local the function made under a wider type is no alias: after the return, the wider type is the only name the value has (const subs: Sub[] = []; ...; return subs from a function declared to return Base[]). Returning a parameter, a field or module-level state is: the narrower name stays reachable.
What the rule can't see
- A side without a written type:
const bases: Base[] = makeSubs(), a value whose type only TypeScript infers, a member of another object (holder.subs). - A generic type declared in another file: whether a value can be written into it isn't known here, so it isn't reported.
- Types that are the same by another route: two aliases of one type in different files are compared as written, and may be reported.
- A local stored somewhere else and then returned (
this.kept = subs; return subsunder a wider return type): a returned local is taken to be the function's own. - A call: covered by
Mut(aSub[]lent asMut<Base[]>is refused by TypeScript), except a function property, which is compared one way (above). - For a function property lent: a side without a written type, or one the checker can't open (a package's type, a generic type, a mapped or conditional type, a parameter without a written type, a rest parameter), and a callee it can't resolve; a parameter narrower only in a primitive (
"a"forstring, a literal union) or in a property's own type more than one level down; an extra property counts as narrowing whether or not it is optional; a function property bound to a writable place without a loan (const narrow: Narrow = wide), which TypeScript accepts the same way.
Related: no-param-mutation, readonly-dropped (the same places, for a readonly property given a writable one); guide: State without globals.