microsoft / microsoft/TypeScript

Suggestion: Upper-bound generic type constraints

Aperta
#9,252 9 commenti 28 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

This is a proposal for generic structural supertype constraints.

I'll first detail the proposal, and then get into a discussion about use-cases.

Proposal

A supertype constraint is a constraint on a generic type parameter to be the structural supertype of another type. It's like a subtype/inheritance constraint, but from the other direction. For example, let's say we have a square:

interface Square {width : number; height : number}

In this suggested syntax, you could write:

performAction<T extended by Square>(partialSquare : T) {}

This means the type T must be a structural supertype of square. So the potential candidates for T are:

{width : number}, {height : number}, Square, {}

It's an established kind of type constraint, not something I just made up. Although it's not exactly common, some languages do implement this feature, such as Scala. In Scala, you can write:

def performAction[T >: Square](obj : T) = { ... }

To express a supertype/upper bound constraint. In this case, of course, the constraint isn't structural -- T must declare that it implements Square.

Utility

In most languages, including Scala, this kind of constraint isn't very useful. It only comes up in certain specific situations.

However, Javascript libraries often have this kind of API, where you're allowed to specify the partial properties of an object to modify it (the rest remain at their previous value).

In many cases, you can support this by having optional interface members, but isn't always possible or correct.

An important example is React, which defines a method called setState:

class Component {
    var state;

    setState(partialState) {
        //merges the properties of partialState with the current state.
    }
}

The right signature for this method should be:

class Component<Props, State> {
    setState<PartialState extended by State>(partialState : PartialState) {
        //merges the properties of partialState with the current state.
    }
}

Currently,the definition files state that it is:

setState(fullState : State) {
    //merges the properties of partialState with the current state.
}

Which doesn't fully capture the functionality of the method.

Notes

The suggested syntax doesn't give us a nice way of combining both subtype and supertype constraints. One possibility is:

exampleMethod<T extends LowerBound and extended by UpperBound>

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

L’issue non fornisce alcun file, test o punto di ingresso per l’implementazione; inizia leggendo la proposta e il relativo esempio React setState, poi individua nel repository le aree relative ai vincoli generici e al controllo dei tipi. Sarebbero necessarie una sintassi e una semantica concordate per i vincoli di supertipo strutturale, inclusa l’interazione con i limiti inferiori, ma l’issue non definisce questi dettagli.

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
20/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.