Skip to content

Rule proposal: Prefer a named interface or type over inline object literals #8320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
6 tasks done
JoshuaKGoldberg opened this issue Jan 30, 2024 · 2 comments
Closed
6 tasks done
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on

Comments

@JoshuaKGoldberg
Copy link
Member

JoshuaKGoldberg commented Jan 30, 2024

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
  • My proposal is not a "formatting rule"; meaning it does not just enforce how code is formatted (whitespace, brace placement, etc).
  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Description

Sometimes folks write inline object type literals to represent objects in type annotations. For example, function parameters; return types; variable declarations. Two problems I see with these inline object literals:

  • They aren't named: so TypeScript errors and refactorings aren't as good
  • They clutter the syntax space around the function

Proposal: let's have a rule that bans object type literals in type annotations, with a configurable minimum number of object properties to report on? Let's say, banning types with >= 2 properties by default?

Fail Cases

let myValue: { apple: boolean; banana: boolean };

Pass Cases

interface Values {
  apple: boolean;
  banana: boolean;
}

let myValues: Values;

Additional Info

I'm not sure what the exact list of syntax locations should be. But "all type annotations" feels like a good start to me.

For auto-fixing, I can imagine this being useful in JSX-oriented libraries such as React if the auto-fixer creates a type with a name like {Component}Props when the type annotation is the first argument of a function that can be a JSX component.

@JoshuaKGoldberg JoshuaKGoldberg added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look enhancement: new plugin rule New rule request for eslint-plugin labels Jan 30, 2024
@bradzacher
Copy link
Member

The issue I have with this is that it is a really, really heavy style to enforce.
Sometimes you do just want a one-off, local, anonymous type for something.
sometimes it doesn't really matter if you have an inline type or not.

EG with react components it's often "fine" to define the prop type inline because nobody wants the type directly unless you're wrapping the component - and TS can do the rename just fine for the component callsites.

Personally I'm a -1 to this living in our project - I think something like #7670 is much more valuable and would be less "opinionated" than this style.

@JoshuaKGoldberg JoshuaKGoldberg added wontfix This will not be worked on and removed triage Waiting for team members to take a look labels Jan 31, 2024
@JoshuaKGoldberg
Copy link
Member Author

Right! It didn't occur to me that #7670 would cover this as well. I'll mark that one in the v7 milestone to make sure we get to it. That rule proposal feels more and more valuable as time goes on. Thanks!

@JoshuaKGoldberg JoshuaKGoldberg closed this as not planned Won't fix, can't repro, duplicate, stale Jan 31, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants