Giter Site home page Giter Site logo

Allow opting out variables about tslint-override HOT 1 OPEN

hmil avatar hmil commented on June 14, 2024
Allow opting out variables

from tslint-override.

Comments (1)

hmil avatar hmil commented on June 14, 2024

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 Cats, not Dogs.
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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.