Repository navigation
Closed
Labels
Needs More InfoThe issue still hasn't been fully clarified
Description
#13487 added default generic types, but it's still not possible to provide a default implementation for a method, which returns the default generic type:
class A {
foo: any;
}
class B extends A {
bar: any;
}
class C<T = A> {
// this is the default implementation of the `create` method
public create(): T {
// Until now the error `Type 'A' is not assignable to type T` is thrown
// To surpress this error you have to make an unsafe upcast to `T`
// e.g. `return new A() as T;`
return new A();
}
}
class D extends C<B> {
// An error should be thrown and the user should be forced to overwrite the default implementation,
// because he provided another Type than the default type `A`
}
class E extends C<A> {
// The default type `A` was specified,
// so the default implementation of the method is fine
}
class F extends C {
// No type was specified and the default type is used,
// so the default implementation of the method is fine
}Here the class C, which has the default type A for its generic type T, provides a default implementation for the method create. If this method is not overwritten it returns an instance of the default type A. Now there are two possible options of extending the class C:
- Another type than the default type
Awas specified: An error should be thrown, that the type of the returned value of thecreatemethod is not assignable to the specified type and the method should be overwritten to return a value of the correct type. - The same type as the default type
Aor no type was specified: The default implementation of thecreatemethod works fine and no error should be thrown.
With this approach unsafe upcast to the generic type T of the return value of the default create method would be prevented.
Reacted by Ivan Hell
Metadata
Metadata
Assignees
Labels
Needs More InfoThe issue still hasn't been fully clarified
Activity
mhegazy commentedon Sep 21, 2017
Default type parameters just allow users of the type to avoid specifying the default on every reference.
In side the case,
Tis still a type parameter and can not be assumed to be the default. remember a class can be instantiated withnewand callers can pass type parameters. so it is legal to write(new C<number>()).create()and expect the result to be anumberas the class contract suggests.Even assuming that these classes has to be
abstract, it is not clear to me what is the relationship between a type parameter and a method definition. you could have multiple type parameters that are optional, e.g.class C<T= number, U = string, V = any>, then which ones require you to redefine a method when you extend? are they the methods that use them in return type? what about parameter types? what about ones that use them implicitly?Joge97 commentedon Sep 28, 2017
It is clear to me, that all types declared by the generic type should equal the generic type and this behaviour would not be changed.
However, I forgot to say, that the desired behaviour with default parameters only applies if the generic type was combined with an extend constraint:
With this approach only public/protected return types and public/protected class fields will be affected. Other type declarations like parameters would persists. Even multiple type parameters would not change the behaviour, because everywhere where a public/protected method, which returns a generic type with an extend constraint and a default type or writes a value, which has the type of the default type, to a class member, the method would no longer be valid and must be overwritten.
So, when exactly a method should be overwritten:
Here is a more real-world scenario to understand it better:
I hope now it is clearer what I mean with a default implementation of methods, which interfere with generic types with an extend constraint and default type.
Maybe another keyword like default should be introduced:
This would make it a lot clearer I think.
mhegazy commentedon Oct 12, 2017
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Joge97 commentedon Oct 13, 2017
Mohamed Hegazy (@mhegazy) Have you reviewed my last comment, before closing the issue?