microsoft / microsoft/TypeScript

Allow inference of class generics when declaring an interface

Aperta
#32,044 1 commento 6 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Search Terms

interface, inference, extending class

Suggestion

I'd like to be able to strongly type an interface such that when it is used on a class that has a generic I can infer the type of that generic to then use in the declaration of the interface.

Use Cases

I want to use this within a library that exposes both an abstract class and an optional interface that developers can opt-in to use. The abstract class has one generic parameter, and in order to correctly type the interface without duplicate generic declaration I want to be able to infer the abstract class' generic type.

Currently the inheriting class declarations must duplicate their generic type declaration.

This issue is similar to https://github.com/microsoft/TypeScript/issues/26242 I think, but I don't see how the resolution of that issue will fix this one.

Examples

abstract class Base<T> {
    // implementation omitted for brevity
}

// ⬇️this is the proposed new syntax ⬇️
interface Remap<T> extends Base<infer U> {
    remap(input: U): T;
}

class Foo extends Base<string> implements Remap<number> {

    public remap(input: string): number {
        return input;
    }

}

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Nell’issue non sono identificati file sorgente, test o punti di ingresso del compilatore. Inizia leggendo la sintassi TypeScript proposta e l’issue correlata #26242, quindi traccia come vengono attualmente verificati l’ereditarietà generica e le dichiarazioni delle interfacce. Il lavoro sarà considerato completato quando il team avrà definito e implementato il comportamento di inferenza previsto, con una copertura per l’esempio mostrato e i relativi casi limite.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.