microsoft / microsoft/TypeScript

implicit 'any' in method with parameter of inferred generic type

Ouverte
#58,726 6 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Domain: check: Type Inference Help Wanted Possible Improvement
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

🔎 Search Terms

implicit 'any' method parameter inferred generic type

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about type inference
⏯ Playground Link

https://www.typescriptlang.org/play/?suppressImplicitAnyIndexErrors=true&ts=5.5.0-dev.20240531#code/C4TwDgpgBAwg9gOwM7AE4FcDGw6oDwAqAfFALxQDeAUFFAhAO5QAUAdOwIaoDmSAXHQgA3CKgDaAXQCUAggG4qAXwVUAlgmCiAZh0zQA4hHqpVmAApcOAWySES1WgCMuA5sFlSyJIXFUATBUUqKlBIKAAZVRRCKAgAD00EPyQoVAgOP0QAGxBKRUkScgcoMQBpKHUoAGsIEDgtKAIJATF4ZDQsHHwCMokiABooQ2NTC1RrWx7SvolAlT8ITCyuaC10BGxVRCgtODgY+MTk1PTMhBy8guYwSxsBSOixdlYmohkoH38VJY4kFIAxPaUGhQTQoZieChBII-P5QABCXDywQA9CioABJBBaURGPQ7DiqLJIKi7ODMMRUNEAPQA-CCxIC4IMKM5UG5IcFaIpFBJ+gzEagWWyOcCQTyJFRpKj0VicWkNtAGLgqiSyRSGUzhVxRcVaMBWGDgBCFNzeVKpMF1JpUDp8QA5dBWRyiMYTOzApwuFjuOhOl2oTykby+AJKYKhaAPYAAJgOCSMxzSGWyuShBTInpK5UqNTqDSaLTaKAw2FwhF6Aygjudrtuk0rsyU80WyzSO3Wm22ZLjBFiCaSKWTZwu6b613r9yisbwT3Yr3enzDVNl2NxiqgytQqtJexjGrRUDpmr22vZwE54t5-NoYkFZ91XKgEotQA

💻 Code
type Constructor<T> = {
  new (...args: never[]): T;
};

interface GenericParams<T> {
  bar: (t: T) => void;
}

type List<T extends readonly {}[]> = {
  [K in keyof T]: [Constructor<T[K]>, GenericParams<T[K]>];
};

declare function foo<T extends readonly {}[]>(params: List<[...T]>): void;

class Foo {
  test() {}
}
class Bar {}

// Inference fails
foo([
//^?
  [Foo, {bar(t) {

  }}],
  [Bar, {bar(t) {

  }}]
])

// Inference works
foo([
  [Foo, {bar(t) {
    t.test();
  }}]
])

interface NumberParams<T> {
  bar: (t: number) => void;
}

type List2<T extends readonly {}[]> = {
  [K in keyof T]: [Constructor<T[K]>, NumberParams<T[K]>];
};

declare function foo2<T extends readonly {}[]>(params: List2<[...T]>): void;

// Inference works
foo2([
// ^?
  [Foo, {bar(t) {

  }}],
  [Bar, {bar(t) {

  }}]
])
🙁 Actual behavior

In the foo example, the parameter t in the associated bar methods are inferred as type any. It seems that TypeScript should be able to infer the actual types of the parameters.

🙂 Expected behavior

t is inferred as type T in each case, i.e. t should be Foo in the Foo case, and Bar in the Bar case.

There are two additional cases provided here: one with a single element in the provided array, and one where the parameters are not generic (number). In both cases, type inference does assign the correct non-any type. It's unclear what exactly the issue is in the first case.

Additional information about the issue

It's possible this is related to #47599, though I don't have enough knowledge of the internals to say whether or not it's the same class of bug. I haven't found another issue reporting this same behaviour.

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 la reproduction liée dans TypeScript Playground et comparez les appels génériques à foo avec les cas à élément unique et non générique de foo2. Suivez le comportement de l’inférence des types autour du type List mappé ; c’est terminé lorsque chaque paramètre bar est inféré comme Foo ou Bar plutôt que comme any, tandis que les autres cas restent corrects.

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é
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Clairement spécifiée
Accessibilité débutants
38/100

Recevez les nouvelles issues par e-mail

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