Comments (1)
I don't want to see a warning/error when I use an overridden variable
It should only warn when you redefine a variable from the parent class, which is a very dangerous behavior. It shouldn't warn when you use it.
There is no mechanism to opt out variables yet. I'm open to discussing the feature if you can provide a compelling use case for it. Please read the note below on why variable overrides are dangerous and should be avoided. Also, it'd be great to see how the TS compiler deals with these cases, and find out if there is some overlap between the compiler and this plugin. If the dangerous cases are already handled by the TS compiler, then it could make sense to add an opt-in setting to exclude variables from the inspection.
Side note: Why is it so dangerous to override member variables?
If the base class defines pet: Animal
, and you override it as pet: Cat
(type narrowing), then what if the base class, needs to set pet = Dog()
? Or if someone casts your CatOwner
into a PetOwner
and gives him a Dog
?
Similarly, say the base class defines pet: Cat
and you override it as pet: Animal
(type widening). Then you may assign pet to a Dog
instance, and then something might blow up in the base class because it assumed pet
to be Cat
s, not Dog
s.
As you can see, neither narrowing nor broadening the type is safe.
Method overrides are safe if the method parameters is a supertype of the base class method parameters, and the return value is a subtype of the base class' method return value. How come this works for functions but not variables?
Functions are generally considered immutable, so you'll expect both the base class' and the class consumers to just "call" the function, not to modify it. It doesn't work for variables because you expect both the base class and the consumers to modify and read the variable, which creates an upper and lower type bound whose only solution is to preserve the type defined by the base class.
However, if the variable is public readonly
(as it should in proper OO/FP design), then overriding it with a more specific type is safe. Because the only statement you'll expect to encounter is val somePet: Pet = yourClass.pet
. You can trivially see that if pet
is more specific, this won't be a problem.
Anyway, as you can see, variable overrides aren't trivial and it is a good thing that this module warns you when they happen. I wonder if the TS compiler already deals with these cases though 🤔
from tslint-override.
Related Issues (20)
- Separate rules for 'check-overrides' and 'explicit-overrides' HOT 2
- Confusing `exclude-interfaces` behaviour HOT 4
- Add support for changing the casing of the decorator for use in the fixer HOT 3
- Rule warns on overloads, and "fixes" overloads to make an *actual* error HOT 1
- Add an option to add a new line after jsDoc style fixes HOT 2
- Support of Project Reference HOT 1
- Replace tslint with typescript-eslint ? HOT 2
- The override decorator is required on anonymous classes, even though they're invalid HOT 2
- Add a "@final" rule? HOT 1
- "new-line-after-decorators-and-tags" not working HOT 2
- Angular like syntax - @Override() HOT 2
- Improve fixers
- `window` cannot be found inside of Web Workers HOT 1
- Ignore methods with 'super' HOT 1
- Support decorator syntax
- Allow opting out of checking for interfaces HOT 4
- Allow opting out of certain function names HOT 1
- Add license
- npm install does not install rules directory HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tslint-override.