microsoft / microsoft/TypeScript
Type inference in index type assignment is skipped
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
TypeScript Version: 3.8.3
Search Terms: index indexed type assignment initialize initializer initialization inference object narrow narrowing assertion
Code
type One = { name: string; };
type Many = Array<One>;
type OneOrMany = One | Many;
var one: OneOrMany = { name: "sam" };
one.name = "pat"
var many: OneOrMany = [{ name: "sam" },{ name: "ash" }];
many[0].name = "pat"
// all is well here
interface OOMDict { [key: string]: OneOrMany; }
var oomdict2: OOMDict = {};
oomdict2.one = { name: "sam" };
oomdict2.one.name = "pat";
oomdict2.many = [{ name: "sam" }, { name: "ash" }];
oomdict2.many[0].name = "pat";
// all is well here
// testing resolution within an index type initializer
var oomdict: OOMDict = { one: { name: "sam" }, many: [{ name: "sam" }, { name: "ash" }] };
oomdict.one.name = "pat";
// ERROR: Property 'name' does not exist on type 'OneOrMany'.
// ERROR: Property 'name' does not exist on type 'Many'.(2339)
oomdict.many[0].name = "pat";
// ERROR: Element implicitly has an 'any' type because expression of type '0' can't be used to index type 'OneOrMany'.
// ERROR: Property '0' does not exist on type 'OneOrMany'.(7053)
Expected behavior: Initialization / assignment of an OOMDict object effects type inference on its OneOrMany values identically to initialization or assignment of a OneOrMany variable.
Actual behavior: Values in an OOMDict either do not narrow based on their contents and type inference (oomdict.many does not narrow from OneOrMany to Many) or they narrow to the wrong specific type (oomdict.one narrows from OneOfMany to Many instead of One).
Related Issues: https://github.com/Microsoft/TypeScript/issues/1706 https://github.com/microsoft/TypeScript/issues/5089
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 con la riproduzione minima nell’issue o nel relativo link Playground, usando il comportamento di TypeScript 3.8.3 come baseline. Confronta il narrowing per l’inizializzazione diretta di OneOrMany con i valori all’interno dell’inizializzatore del tipo indice di OOMDict. Il lavoro è completato quando le due assegnazioni di proprietà di OOMDict vengono ristrette come le assegnazioni dirette e non rimangono errori di tipo segnalati.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100