Skip to content

Rule proposal: Enforce related types for getter and setter pairs #7236

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 Jul 15, 2023 · 1 comment · Fixed by #10192
Closed
6 tasks done

Rule proposal: Enforce related types for getter and setter pairs #7236

JoshuaKGoldberg opened this issue Jul 15, 2023 · 1 comment · Fixed by #10192
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: new plugin rule New rule request for eslint-plugin locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@JoshuaKGoldberg
Copy link
Member

JoshuaKGoldberg commented Jul 15, 2023

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

TypeScript 5.1 allowed getter and setter accessor pairs to specify two different types. While this may be a necessary change to represent JS APIs that use unrelated types (see the release notes), it feels like a confusing thing to do in code. I think it'd make sense to have a lint rule that enforces a getter's type at least be a subtype of its setter's type - as TypeScript used to enforce.

Fail Cases

interface Example {
  get value(): string;
  set value(input: string | undefined);
}

Pass Cases

interface Example {
  get value(): string;
  set value(input: string);
}

Additional Info

It might be useful to have an option to enforce whether the types must be the exact same. E.g. the super-type being a number | string while the sub-type is string.

Blocked on my favorite TypeScript issue, microsoft/TypeScript#9879: adding a Type Relationship API. Filing this for tracking and because I enjoy passive-aggressively bugging the TypeScript team for features. ❤️

@JoshuaKGoldberg JoshuaKGoldberg added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin enhancement: new plugin rule New rule request for eslint-plugin blocked by external API Blocked by a tool we depend on exposing an API, such as TypeScript's Type Relationship API labels Jul 15, 2023
@JoshuaKGoldberg
Copy link
Member Author

Now that v8 is released, per #7936, we can mark this issue as unblocked! 🚀

@JoshuaKGoldberg JoshuaKGoldberg added accepting prs Go ahead, send a pull request that resolves this issue and removed blocked by external API Blocked by a tool we depend on exposing an API, such as TypeScript's Type Relationship API labels Aug 1, 2024
@github-actions github-actions bot added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Nov 19, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: new plugin rule New rule request for eslint-plugin locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant