Skip to content

MohannadNaj/eslint-plugin-ts-inline-parameter-types

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

eslint-plugin-ts-inline-parameter-types

ESLint plugin that flags single-use type definitions in function parameters.

Installation

npm install -d eslint-plugin-ts-inline-parameter-types

#     bun add -d eslint-plugin-ts-inline-parameter-types
import tsInlineParameterTypes from 'eslint-plugin-ts-inline-parameter-types';

export default [
  {
    plugins: {
      'ts-inline-parameter-types': tsInlineParameterTypes,
    },
    rules: {
      'ts-inline-parameter-types/prefer-inline-type-parameters': 'warn',
    },
  },
];

Rule: prefer-inline-type-parameters

Warns when a type is defined separately but only used once in a function parameter.

❌ 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.


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

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 lint

License

MIT

About

ESLint plugin to enforce inline TypeScript types in function parameters -if the type was only used once.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors