microsoft / microsoft/TypeScript
Feat: infer parameter types for the implementation signature of an overloaded method
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
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.
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 den motivierenden Beispielen für überladene Methoden in diesem Issue und lies dann die verwandten Issues #7763 und #34319. Bestimme die beabsichtigten Inferenzregeln für Implementierungsparameter und wie sich Narrowing über Overloads hinweg verhalten sollte; abgeschlossen ist die Aufgabe, wenn das Verhalten ausreichend genau spezifiziert ist, um die Implementierung und Tests anzuleiten.
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