microsoft / microsoft/TypeScript
Type inference only works on the same line as the variable declaration
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
I ran into this odd behavior while writing some tests, that do the typical describe, declare a closure-scope variable, and in beforeEach, assign some object to that variable that is used in each of the tests inside.
Search Terms:
infer, inference, closure, scope, broken, any, cast
Initially, I thought it was limited to closure scope, but I then started reducing the reproduction sample more and more, and realized, it's broken even if it's on the very next line:
Code
// given
interface Foo {
id: string;
}
function make(): Foo {
return {
id: 'akatechis',
}
}
// u1 is correctly inferred to be of type Foo.
let u1 = make();
// u2 is incorrectly inferred to be of type any.
let u2;
u2 = make();
// this was the scenario in which I originally ran into this:
// a closure scoped variable that is assigned in an inner function
function outer () {
// u3 is inferred to be of type any
let u3;
return {
inner() {
// u3 here is still any
u3 = make();
// surprisingly, returning u3 here causes the return type of inner() to be Foo, not any???
return u3;
}
}
}
Expected type declarations:
interface Foo {
id: string;
}
declare function make(): Foo;
declare let u1: Foo;
declare let u2: Foo; // <---- this should have been inferred to be Foo
declare function outer(): {
inner(): Foo;
};
Actual type declarations:
interface Foo {
id: string;
}
declare function make(): Foo;
declare let u1: Foo;
declare let u2: any;
declare function outer(): {
inner(): Foo;
};
Playground Link: playground
Related Issues:
The only thing that I found somewhat close to this issue was the question in the FAQ about unused generic types causing type widening, but I don't think this is it, because I'm not using generics here.
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 déclarations inférées pour u1, u2 et outer(). Lisez l’entrée correspondante de la FAQ sur le type widening afin d’écarter cette explication, puis identifiez le comportement d’inférence des types responsable de la différence. C’est terminé lorsque u2 est inféré comme Foo tout en conservant le résultat affiché de outer().
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
- 35/100