microsoft / microsoft/TypeScript
Improve Javascript intellisense type inference for cases where Object.assign(this, ...) is used with an object with known type information
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die bereitgestellte JavaScript-Klasse und das Object.assign-Beispiel im TypeScript-Checker oder in den Editor-Tools nachzubilden; verfolge dann, wie der bekannte Typ des letzten Arguments behandelt wird. Als erledigt gilt die Aufgabe, wenn beim Überfahren von f.foo und f.bar bei beiden string angezeigt wird und die dokumentierten Vorbehalte für unbekannte, bedingte oder spätere Zuweisungen erhalten bleiben.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100