AbsoluteJS

absolute/no-inline-object-types

Disallow inline object type literals on annotations; prefer extracting them to a named type alias.

TypeScriptSuggestion
View on GitHub

#Rule Details

Disallows inline object type literals in annotation positions and nudges them toward a named type alias. A named shape is reusable, shows up by name in tooltips and errors, and keeps signatures readable. The rule descends through wrappers — T[], Array<T>, Promise<T>, Record<K, V>, Map<K, V>, unions, and intersections — so the inner shape is still caught.

It checks variable annotations, class field annotations, function/method/constructor parameter annotations, and explicit generic type arguments on calls and new expressions. A literal is only flagged once it has at least minProperties members (default 2), and index-signature-only literals are skipped because they are really Record shapes. as-casts are not annotations and are left alone.

#Examples

Incorrect
TS
const config: { item: string; test: number } = { item: 'a', test: 1 };
const items: Array<{ id: string; name: string }> = [];
const lookup: Record<string, { id: string; name: string }> = {};

function update(opts: { delay: number; retries: number }) {
	return opts;
}
Correct

Extract to a named alias

TS
type User = { name: string; age: number };
const user: User = { name: 'Ada', age: 36 };

type Item = { id: string; name: string };
const items: Item[] = [];
TS
const point: { x: number } = { x: 1 };          // single property < threshold 2
const dict: { [key: string]: number } = {};      // index signature onlyRecord-shaped
const value = input as { a: string; b: number }; // an as-cast is not an annotation

#Options

This rule accepts the following options:

OptionTypeDefaultDescription
minPropertiesnumber2Minimum number of members an inline object type must have before it is flagged. Set to 1 to also catch single-property literals.
JS
{
	rules: {
		// Only flag inline object types with 3+ members
		'absolute/no-inline-object-types': ['warn', { minProperties: 3 }]
	}
}

#When Not To Use It

Small, local callback signatures sometimes read better with an inline shape than with a one-off named alias. Raise minProperties if you only care about larger shapes, or disable the rule where inline types are idiomatic.

#Resources