microsoft / microsoft/TypeScript
implicit 'any' in method with parameter of inferred generic type
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
🔎 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
💻 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
- 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 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