microsoft / microsoft/TypeScript
Improve Javascript intellisense type inference for cases where Object.assign(this, ...) is used with an object with known type information
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo la classe JavaScript e l’esempio Object.assign forniti nel checker TypeScript o negli strumenti dell’editor, quindi segui come viene gestito il tipo noto dell’argomento finale. Il lavoro è completato quando passando il cursore su f.foo e f.bar viene mostrato string in entrambi i casi, preservando le eccezioni documentate per le assegnazioni sconosciute, condizionali o successive.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100