Skip to content

No Runtime in Type Files

Error
small-rules/no-runtime-in-type-files

Disallow runtime code in files that hold only types.

Rationale

Some projects exempt type files from their test rules by filename. That’s only safe while the files contain nothing that runs. Runtime code added there escapes its tests. This rule reports each top-level statement in a matching file that stays in the output. That covers values, functions, classes, enums, namespaces, bare imports, and imports or exports of values. Type imports and exports pass, and so do interfaces, type aliases, function signatures, and declare forms.

files holds the glob patterns for those files, matched against the full path. The default, **/packages/*/src/lib/**/*-types.ts, covers *-types.ts modules under a package’s src/lib.

Diagnostic Messages

noRuntimeInTypeFile
Type files are exempt from runtime tests because they must erase completely. Move this runtime code to a tested source module.

Configuration

This rule accepts one options object after the severity.

filesOptional

Glob patterns for the files that may hold only declarations that erase, matched against the full path

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/no-runtime-in-type-files": [
"error",
{
"files": [
"**/packages/*/src/lib/**/*-types.ts"
]
}
]
}
}

Examples

Runtime binding in a type file
import type { Input } from "./input";
export const value = 1;
Declarations that erase
import type { Input } from "./input";
declare const brand: unique symbol;
export interface Output { readonly value: Input; }
export type Branded = Output & { readonly [brand]: true };