eslint-plugin-prefer-inline-types
v1.0.3
Published
ESLint plugin to enforce inline TypeScript types in function parameters
Maintainers
Readme
eslint-plugin-prefer-inline-types
ESLint plugin that flags single-use type definitions so you can inline them for clarity. You can limit the rule to function parameters only or apply it everywhere (return types, variables, class properties, etc.).
Installation
npm install -D eslint-plugin-prefer-inline-types
# or
pnpm add -D eslint-plugin-prefer-inline-typesimport preferInlineTypes from 'eslint-plugin-prefer-inline-types';
export default [
{
plugins: {
'prefer-inline-types': preferInlineTypes,
},
rules: {
'prefer-inline-types/prefer-inline-type-parameters': 'warn',
// Or limit to function parameters only:
// 'prefer-inline-types/prefer-inline-type-parameters': ['warn', { scope: 'parameters' }],
},
},
];Rule: prefer-inline-type-parameters
Warns when a type is defined separately but only used once. Supports both non-generic and generic type aliases and interfaces. Also fixes types used nested inside other types (e.g. Map<string, PushStatus>).
Options
| Option | Values | Default | Description |
| -------- | -------------- | ------------ | ----------- |
| scope | "parameters", "everywhere" | "everywhere" | "parameters" — only report types that are used once in a function parameter. "everywhere" — report any type that is used exactly once (parameters, return types, variables, class properties, etc.). |
❌ Incorrect
type UserProps = {
name: string;
age: number;
};
const User = ({ name, age }: UserProps) => {
return <div>{name}</div>;
};interface Config {
apiUrl: string;
timeout: number;
}
function initApi(config: Config) {
return config.apiUrl;
}✅ Correct
const User = ({ name, age }: {
name: string;
age: number;
}) => {
return <div>{name}</div>;
};function initApi(config: {
apiUrl: string;
timeout: number;
}) {
return config.apiUrl;
}Auto-fix handles this transformation automatically (including nested usage like Map<string, MyType>).
Skipped cases
Type is reused
type UserProps = {
name: string;
};
const UserA = ({ name }: UserProps) => <div>{name}</div>;
const UserB = ({ name }: UserProps) => <span>{name}</span>;Type is exported
export type UserProps = {
name: string;
};
export const User = ({ name }: UserProps) => {
return <div>{name}</div>;
};Type used in non-parameter context (only when scope: "parameters")
When scope is "parameters" (default is "everywhere"), single-use types in variables, return types, or class properties are not reported:
type Config = {
enabled: boolean;
};
const config: Config = { enabled: true };Type used in multiple contexts
type Config = {
enabled: boolean;
};
const config: Config = { enabled: true };
function updateConfig(newConfig: Config) {
// ...
}Development
bun install
bun test
bun run format
bun run lintOr with npm:
npm install
npm testPublishing to npm
- Log in:
npm login - Bump version in
package.jsonif needed (e.g.1.0.2) - Publish:
npm publish(ornpm publish --access publicfor scoped packages)
Credits
- Original plugin by MohannadNaj.
This fork adds three improvements on top of the original:
Generic type support — The original skips type aliases and interfaces that have type parameters, so e.g.
type ConnectDeps<D, S, O> = { ... }used once in a parameter was never reported. Here, generic types are included: if such a type is used only once in a function parameter, the rule reports it and the fix inlines the type body as in the non-generic case.Nested type autofix — The original fix only runs when the type is the direct parameter type (
param: MyType). If the type appears inside another type (e.g.param: Map<string, PushStatus>ortx: IDBPTransaction<..., StoreNames, ...>), the fix bailed out and did nothing. This version also inlines in those cases by replacing the nested type reference with the type body instead of replacing the whole annotation.TypeScript declarations — A
lib/index.d.tsis shipped so that projects using the plugin get typed imports and do not need// @ts-expect-errorwhen adding it to their ESLint config.Configurable scope — Option
scope: "parameters" | "everywhere"(default"everywhere"). Use"parameters"to only flag single-use types in function parameters; use"everywhere"to flag them in any position (return types, variables, class properties, etc.).
License
MIT
