microsoft / microsoft/TypeScript

Feat: infer parameter types for the implementation signature of an overloaded method

Aperta
#55,236 3 commenti 0 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

Suggestion

implicitly infer parameter types for the implementation signature of an overloaded method
This issue is a complement to #7763, #34319

🔍 Search Terms

TypeScript interface with multiple signatures;
infer params from different signatures;
implementation signature of an overloaded method;

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Add an implicit inference to the params or restricted inference in some cases

📃 Motivating Example

Before, you can achieve simple

class Some {
  async request(action: 'auth', payload: AuthPayload): Promise<AuthResponse>
  async request(action: 'pub', payload: PubPayload): Promise<PubResponse>
  async request<S extends Values<SignalingRequests>>(
    ...params: Parameters<S>
  ): Promise<ReturnType<S>> {
    const [action, payload] = params
    switch (action) { /* ... */ }
  }
}
// redundant declarations
// declare Values<T> = T[keyof T]
declare type SignalingRequests = {
  auth: (action: 'auth', payload: AuthPayload) => AuthResponse
  pub: (action: 'pub', payload: PubPayload) => PubResponse
  // ...
}

Now you can use

class Some {
  async request(action: 'auth', payload: AuthPayload): Promise<AuthResponse>
  async request(action: 'pub', payload: PubPayload): Promise<PubResponse>
  async request(...params) {
    if (params[0] == 'auth') {
      const p: AuthPayload = params[1] // this works
    }
    // or you can write:
    const [action, payload] = params
    switch (action) {
      case 'auth':
        payload // AuthPayload
        break
      case 'pub':
        break
      default:
        // exhaustive check
        const nope: never = action
        throw 'nope'
    }
  }
}

💻 Use Cases

Prevent complex type annotation workarounds, simplify codes, make TypeScript sound again!

Best regards.

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

Inizia con gli esempi motivanti di metodi sovraccaricati in questo issue, poi leggi gli issue correlati #7763 e #34319. Determina le regole di inferenza previste per i parametri di implementazione e come dovrebbe comportarsi il narrowing tra gli overload; il lavoro è concluso quando il comportamento è specificato abbastanza bene da guidare l'implementazione e i test.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.