Support final classes (non-subclassable)
I was thinking it could be useful to have a way to specify that a class should not be subclassed, so that the compiler would warn the user on compilation if it sees another class extending the original one.
On Java a class marked with final cannot be extended, so with the same keyword on TypeScript it would look like this:
final class foo {
constructor() {
}
}
class bar extends foo { // Error: foo is final and cannot be extended
constructor() {
super();
}
}
@RyanCavanaugh While there's a good example with render() wanting final, no one has mentioned the open-closed principle (now acronymed up in second place as part of SOLID). For writing frameworks that others will use this would be useful. If not, what is the preferred approach for taking the open-closed approach in Typescript?
I would LOVE a final for methods (and I suppose properties and declarations in general). It's causing real trouble - people keep overriding things accidentally not realising that there was one underneath... breaking stuff...
I also think that final methods would be of great value, and as others have pointed out, all of the arguments against seem to focus on final classes. There was a separate issue (#9264) but it was closed because apparently this issue covers it, but I really don't think it does. Could we either re-open that issue, or properly address it here?
My situation is similar to @0815fox where I have an "inner" version of a method, which is abstract, but I want the "original" version of the method to never be overridden, since it contains important behaviour and I don't want a subclass to override that one.
I find it baffling that no matter the overwhelming support of the community in favor of this feature, it is still being declined by the TypeScript developers...
perhaps a stop gap would be to add a standard to TS-Doc where it would be advised that someone not inherit a particular class or override a particular method. Like a @final annotation or something similar.
Well, what should I add to what all the others, voting for "final", have said... Here is my vote for it.
Agreed. Sure, you can use readonly with methods if you'll treat it like a property of type function, but this workaround can be pretty confusing. Would be nice to have a native solution for this problem.
I think final , especially final method is important in type checking, as it often controls the workflow of the methods, which guarantee the order of method called in Template pattern or other design patterns.
final should be the way to go
Gang - since this ticket is (a) closed, and (b) has a misleading title for the "final method" discussions I decided to create a new ticket focused solely on final methods. Hopefully that will be harder to ignore and may even stimulate some progress. Oh yeah see https://github.com/microsoft/TypeScript/issues/33446
@RyanCavanaugh
complexity vs value bar
Could you elaborate on what is so complex about final? Seems to me like it would be one of the easiest keywords to implement, especially given that keywords such as abstract are already implemented, and a lot of the logic/code could be reused from that.
@vinayluzrao
Just as a random example, currently we say that you can inherit from anything that has a construct signature:
// OK
declare const SomeParent: new() => { a: string };
class A extends SomeParent { }
Obviously a final class is new-able:
function make<T>(ctor: new() => T) {
return ctor;
}
final class Done {
}
// No problem
const d = make(Done); // d: Done
But now there's a contradiction: We have a thing that's new-able, but isn't legal to put in an extends clause. So one level of indirection defeats final:
function subvertFinal(ctor: new() => any) {
class Mwhahaah extends ctor {
constructor() {
super();
console.log("I extended a final class! So much for 'types'!")
}
}
}
final class Done {
}
subvertFinal(Done);
This already people trips people up on abstract and folks are constantly arguing that abstract classes should have some implicit construct signature that exists for typechecking purposes but isn't actually invokeable or something.
FYI 90% of the time when we say "complexity" in these discussions, we mean conceptual complexity as experienced by users of TypeScript, not complexity of implementation from our side as developers of the product. The latter is something that can be dealt with (conditional types and mapped types are extraordinarily complex from an implementation perspective, for example); conceptual complexity isn't something you can deal with by throwing more and smarter engineers at the product. Every time we add something to the language that causes unexpected or hard-to-explain behavior, it's a mental burden on every user of the language.
I just need final for methods, because on a relatively regular basis, someone overrides (without knowing it) a method underneath, therefore my code doesn't run, and things break in very strange ways, and people don't know why, and it often takes a long time to debug.
@RyanCavanaugh that really clarifies it, thanks. Though, I would like to throw out a thought : it seems to me that the problem in the example you gave arises due to the strong coupling between new and extends, perhaps that is the larger problem, and not final itself. I wonder why this coupling decision was made, and how hard it would be to undo it (not at all implying it should be undone) at this point.
I'm not sure if this might be too tangential a discussion to have here, though.
This is an issue I ran into. Such simple code sample cannot be compiled.
final keyword would PERFECTLY solve it:
abstract class Parent {
public abstract method(): this;
}
class Child extends Parent {
public method(): this {
return new Child();
}
}
Type 'Child' is not assignable to type 'this'. 'Child' is assignable to the constraint of type 'this', but 'this' could be instantiated with a different subtype of constraint 'Child'.
can this be reopened please? it is obvious that there's a need for this feature
We've discussed this issue about a dozen times at various meetings and every time decided against it. Reopening this to discuss a thirteenth time is not a good use of our time.
You can disagree with the TypeScript team's decisions, which is fine, but we can't spend all day going around in circles re-re-re-re-re-re-re-deciding decisions that people disagree with. There are literally a thousand other suggestions in this repo that deserve initial consideration before this one gets to be brought up again.
Constructive discussion like @nicky1038's comment is very welcomed here; I would request that people not continually ping this thread for news/reconsideration requests so that we don't have to lock it.
I'm here to support adding final for methods as well. I have a base class with a function called init that I do not want subclasses to be able to override. Final would be the perfect solution.
I also support adding the final keyword for methods.
Nowadays it is a standard in object oriented programming.
Adding final makes sense to me. Especially when building libraries/frameworks for others to use, where you don't want to even allow the risk of overriding a function.