microsoft / microsoft/TypeScript
Reusing function type with different parameters amount (overloading)
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
Bug Report
Is there any reason it's not possible to re-use a function type that is overloaded aka has different possible amount of arguments?
In the playground I put some code that shows this issue, but I will put a quick snippet here:
declare function foo(c: {a: number; b: string}): void
declare function foo(a: number, b: string): void
foo({ a: 1, b: "2" })
foo(1, "2")
// This doesn't work but is relatively understandable
const bar1: typeof foo = (a_c, b) => {
a_c
b
}
// This should work, why it doesn't?
const bar2: typeof foo = (...args) => {
args
}
// it works fine, but you can't access the second parameter
const bar3: typeof foo = (a_c) => {
a_c
}
// it works fine, but you can't access the parameters
const bar4: typeof foo = () => {
}
bar2 in this example doesn't work, and the error that is given is Type '(args_0: number | { a: number; b: string; }, args_1: unknown) => void' is not assignable to type '{ (c: { a: number; b: string; }): void; (a: number, b: string): void; }'.
🔎 Search Terms
arguments overload parameter function
🕗 Version & Regression Information
In 4.4.0-beta & 4.3.5:

In 4.2.3 and before:

⏯ Playground Link
🙁 Actual behavior
It just errors out when trying to use "...args" to get all the possible arguments of the function
🙂 Expected behavior
To be able to use "...args" or similar to get the arguments and typescript to not complain just for trying to use it
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par reproduire le comportement d’affectation des surcharges dans le TypeScript Playground lié, en utilisant les versions signalées 4.3.5 et 4.4.0-beta. Comparez les exemples acceptés et rejetés, puis déterminez le comportement attendu pour les implémentations à paramètres rest de fonctions surchargées ; le travail est terminé lorsque le comportement et le typage attendu sont établis et couverts par un test approprié du compilateur.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Bug
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 25/100