microsoft / microsoft/TypeScript
Generic of abstract class should be inferrable from abstract property implemented in subclass
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par la reproduction TypeScript dans l’issue et comparez la forme générique explicite avec la forme proposée class Gender extends SomeMapper. C’est terminé lorsque la déclaration proposée passe la vérification des types et déduit le générique à partir de la propriété abstraite implémentée, sans nécessiter d’argument de type explicite.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Clairement spécifiée
- Accessibilité débutants
- 35/100