Generic of abstract class should be inferrable from abstract property implemented in subclass
See original GitHub issueSearch Terms
- generic
- abstract class
- abstract property
- type inference
Suggestion, Use Case and Examples
As of typescript 3.9.3, if I write a code like the following:
abstract class SomeMapper<T extends string> {
private someMap = new Map<number, T>();
protected abstract readonly rndText: T;
getSome(num: number) {
return this.someMap.get(num)!;
}
}
function rndGender(): 'male' | 'female' {
return Math.random() > 0.5 ? 'male' : 'female';
}
class Gender extends SomeMapper<'male' | 'female' /* need to provide this explicitly */> {
readonly rndText = rndGender();
constructor() {
super();
}
}
const gen = new Gender();
const some = gen.getSome(1);
I have to make sure I always provide the generic to the SomeMapper class, and if I ever update the type of rndText
in Gender
class, I’ll have to go and update the generic as well every time. The only way as of now to make this kinda auto-infer the generic, is to write it like this:
class Gender extends SomeMapper<Gender['rndText']> {
readonly rndText = rndGender();
constructor() {
super();
}
}
However this is not the most elegant way and can get very dirty with complicated type (for example check this), and typescript should be able to do this on its own, similar to what it does with generics in functions.
So the ideal behaviour should be simply like this:
// Below line should not error: Generic type 'SomeMapper<T>' requires 1 type argument(s).
// Should auto infer the generic from property rndText
class Gender extends SomeMapper {
readonly rndText = rndGender();
constructor() {
super();
}
}
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:8 (3 by maintainers)
Top Results From Across the Web
How to define abstract property based on generic type
What I'm trying to do is to define type of a property in abstract class and initialize it in child ...
Read more >Generics & Abstract Classes in TypeScript - Medium
Multiple generics can be passed into a function or class as well, for a function passed generics <T, K> would have access to...
Read more >Abstract Classes
In this lecture we will compare abstract classes to interfaces (which seem closely related: for example we can extend interfaces via inheritance too)...
Read more >C# Abstract Class - C# Tutorial
The subclasses of an abstract class must implement the abstract methods and properties. Was this tutorial helpful ?
Read more >The Ceylon Language
A class or interface may declare type parameters, which abstract the definition of ... eliminating the need to repeatedly specify generic type arguments....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is going to be a fairly invasive change with a lot of potential downstream breaking effects. We’d like to make sure that this is affecting more than one person before changing the inference algorithm, hence “Awaiting More Feedback”
This could definitely be a useful feature, especially in some rather complex cases I’m encountering in Angular
Ideally I could magically infer
TFormControls
viaform
property here rather than having to do that manually.