microsoft / microsoft/TypeScript

Dependent arguments assignability to overloaded functions

Aperta
#52,303 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Suggestion

🔍 Search Terms

overloads, dependent, tuple signatures

✅ 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

It would be like if dependent parameters (declared using tuple types) could satisfy matching overloads

📃 Motivating Example

TS playground

import * as React from 'react'

type Brand<T, Tag extends string> = T & { __tag: Tag };

export type UniqueNodeId = Brand<string, 'UniqueNodeId'>;
export type UniqueEdgeId = Brand<string, 'UniqueEdgeId'>;

type Dispose = () => void;

interface Observe {
  (type: 'node', uniqueId: UniqueNodeId, element: Element): Dispose;
  (type: 'edge', uniqueId: UniqueEdgeId, element: Element): Dispose;
}

const ctx = React.createContext<Observe>(() => () => void 0);

export function useResize(
  ...[type, uniqueId, ref]:
    | [type: 'node', uniqueId: UniqueNodeId, ref: React.RefObject<Element>]
    | [type: 'edge', uniqueId: UniqueEdgeId, ref: React.RefObject<Element>]
) {
  const observe = React.useContext(ctx)

  React.useLayoutEffect(() => {
    const element = ref.current!;
    return observe(type, uniqueId, element); // this is currently an error
  }, [type, uniqueId, ref, observe]);
}

💻 Use Cases

It seems like a natural extension of the dependent parameters improvements that shipped in TS 4.6. Overloads are often at odds with some TS features though (like with conditional types) so I'm not sure if you want to work on improvements related to them.

The nice thing about overloads is that they don't require extra binding patterns etc to be used - whereas declaring parameters with tuple types pollutes the runtime code and displays differently in the IDE tooltips etc (although sometimes the tooltips act as if the tuple-based signature would be an overloaded one).

Perhaps it would be good to have a documentation page explaining the difference, cons and pros of each approach? It also could suggest which one is better is better in the "modern" TS.

I can somewhat workaround this (TS playground) but the slight problem with this solution is that it requires me to reorder the parameters (to what I think is less intuitive to the caller, in my context). I could use tuples with trailing fixed elements but there is no good runtime way of unpacking such elements that wouldn't require extra set of helpers.

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 dall’esempio collegato di TypeScript Playground e confronta la chiamata con parametri di tupla dipendenti con la firma sovraccaricata di Observe. Analizza come viene verificata l’assegnabilità degli overload e definisci il completamento come l’accettazione delle chiamate node ed edge corrispondenti senza modificare il JavaScript emesso.

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.