ember-cli / eslint-plugin-ember

An ESLint plugin that provides set of rules for Ember applications based on commonly known good practices.
MIT License
257 stars 198 forks source link

New rule proposal: no-tracked-declare #2152

Open steveszc opened 2 weeks ago

steveszc commented 2 weeks ago

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 commented 2 weeks ago

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