microsoft / microsoft/TypeScript

Reusing function type with different parameters amount (overloading)

Ouverte
#45,153 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Bug Domain: check: Variance Relationships
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:

image

In 4.2.3 and before:

image

⏯ Playground Link

https://www.typescriptlang.org/play?target=99&jsx=0&ts=4.3.5#code/CYUwxgNghgTiAEAzArgOzAFwJYHtVJxwAowAueAbynNWQFsAjEGAbngfIGcMYtUBzAL4BKcgDccWYAFgAUKEiwEKdNjwFi1eLUbMANOy48+-UfAlS5V2YkJEK8LQEYDHeACIATO-gjrt4hcPb2FrAHow+AAVAAssTnhgHBBOVAByDHgAdxwYAGt2ZEz4+DhobDEQCABPeDRQGG4oVGAoBggQOTA8bnZYJ3IMaoAHEBxEDXgAXngiKAB9MFdhaYA+Sjl4LcdFze2GOUFwyNiSzhicZAhgbNy8gyyY2qxMpJT0jAB+Lp7MhlhPIMRmMJgFprMAHRQ2D8TgrKbrCh7LYwziHY7wF63fIJRB8ECuIrwaqXeBgZoZRxgMApBIYGIITjgPA3YawKB0EAYZg-VC9f4wADMQNG40mMzmi3hiK2yJ2YHRsjkEUxmRyOKQ+MJmRJyDJFMyUGptPg9IQbJgHK5zDRsm6fL+sAALCKQeLZtKNrIjrIgA

🙁 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

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. 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

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.