microsoft / microsoft/TypeScript
Improve Javascript intellisense type inference for cases where Object.assign(this, ...) is used with an object with known type information
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
From @jj101k on May 28, 2017 10:16
- VSCode Version: 1.12.2 (19222cdc84ce72202478ba1cec5cb557b71163de)
- OS Version: macOS Sierra 10.12.5 (16F73)
Given the Javascript code:
class Foo {
constructor() {
Object.assign(
this,
{
bar: "abc",
}
);
this.foo = "def";
}
}
var f = new Foo();
console.log(f.foo);
console.log(f.bar);
If you hover over f.foo, it will tell you that it's a string. If you hover over f.bar, it will say it's "any". They should both say "string".
Object.assign is a fairly common way of setting several properties with a bit less copy/paste. In cases where an object without known type information is used as the last argument it isn't possible to statically infer appropriate types for the modified object ("this", here) and equivalently the object that Object.assign returns. Where type information of the last argument is known, it is safe and appropriate to import all of that into the type information for the object, as if a series of direct assignments had been done.
Caveats: In Javascript (perhaps not Typescript) it's possible that further unknown properties are present on any given object, so it would be appropriate to void all inferred type information which is not between Object.assign and the end of the constructor. If Object.assign is used outside a constructor (on a named object) it should void all inferred types entirely, because the type information could be entirely different before and after. The same should be true if Object.assign is conditionally called in the constructor. The only exception should be when all arguments after the first have known final type information, eg. an immediate object.
For practical purposes, Object.assign(foo, bar, {baz: 1}) should be considered equivalent to for(name in bar) if(bar.hasOwnProperty(name)) foo[name] = bar[name]; foo.baz=1;.
Copied from original issue: Microsoft/vscode#27397
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Empieza reproduciendo la clase de JavaScript y el ejemplo de Object.assign proporcionados en el comprobador de TypeScript o en las herramientas del editor; después, sigue cómo se maneja el tipo conocido del argumento final. Se considera terminado cuando al pasar el cursor sobre f.foo y f.bar se muestre string en ambos casos, preservando las salvedades documentadas para asignaciones desconocidas, condicionales o posteriores.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript, typescript
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100