microsoft / microsoft/TypeScript

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

Open
#39,180 11 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the TypeScript reproduction in the issue and compare the explicit generic form with the proposed class Gender extends SomeMapper form. No source file or test is named; done means the proposed declaration type-checks and infers the generic from the implemented abstract property without requiring an explicit type argument.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.