Skip to content
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

New rule proposal: no-tracked-declare #2152

Open
steveszc opened this issue Jun 13, 2024 · 1 comment
Open

New rule proposal: no-tracked-declare #2152

steveszc opened this issue Jun 13, 2024 · 1 comment
Labels
enhancement New Rule Idea for a new lint rule

Comments

@steveszc
Copy link

I have seen some typescript ember code that incorrectly uses the declare keyword with @tracked properties like so:

@tracked declare name: string;

get formattedName() {
  return this.name.toUpperCase()
}

Typescript will happily compile this code even though it is incorrectly typed and will result in runtime errors since name is initialized to undefined. There is really no good use-case for using @tracked declare but it can be incorrectly used to silence the typescript compiler in situations where the @tracked property is not initialized with a value (I'm sure there are other ways this could be abused or cause problems beyond this simple example).

Since declare is regularly and correctly used with Ember's @service decorator it can be unclear to Ember developers who are new to typescript or just unfamiliar with the declare keyword that this particular use of declare is harmful. For this reason, I think we should lint against the use of typescript's declare keyword with the @tracked decorator.

@steveszc
Copy link
Author

I should add that I am planning to write a lint rule for this and add it to my company's internal eslint plugin, so I would be happy to contribute that work in a PR here if there is agreement that we want to add this rule to eslint-plugin-ember

@bmish bmish added enhancement New Rule Idea for a new lint rule labels Jun 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New Rule Idea for a new lint rule
Projects
None yet
Development

No branches or pull requests

2 participants