microsoft / microsoft/TypeScript

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

Offen
#58,726 6 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Domain: check: Type Inference Help Wanted Possible Improvement
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der verknüpften TypeScript Playground-Reproduktion und vergleiche die generischen foo-Aufrufe mit den foo2-Fällen mit einem einzelnen Element und ohne Generics. Verfolge das Verhalten der Typinferenz rund um den gemappten List-Typ; fertig ist es, wenn jeder bar-Parameter als Foo oder Bar statt als any inferiert wird und die anderen Fälle weiterhin korrekt bleiben.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.