microsoft / microsoft/TypeScript
Dependent arguments assignability to overloaded functions
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem verknüpften TypeScript Playground-Beispiel und vergleiche den Aufruf mit abhängigen Tupelparametern mit der überladenen Observe-Signatur. Untersuche, wie die Zuweisbarkeit von Überladungen geprüft wird, und definiere die Fertigstellung als das Akzeptieren der passenden node- und edge-Aufrufe, ohne das erzeugte JavaScript zu ändern.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100