Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generic of abstract class should be inferrable from abstract property implemented in subclass

See original GitHub issue

Search 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() {

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() {

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() {


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:open
  • Created 3 years ago
  • Reactions:4
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

RyanCavanaughcommented, Jun 26, 2020

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”

jonapgartwohatcommented, Nov 23, 2022

This could definitely be a useful feature, especially in some rather complex cases I’m encountering in Angular

abstract class Component<TData, TFormControls> {
  abstract form: FormGroup<TFormControls>;
  abstract convertFromForm: (formData:  typeof this.form.value) => TData;
  abstract convertToForm: (data: TData) =>  Parameters<typeof this.form.setValue>[0];

Ideally I could magically infer TFormControls via form property here rather than having to do that manually.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found