microsoft / microsoft/TypeScript

Casting of a union type variable doesn't work if interface has all optional properties

Aperta
#15,289 11 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

TypeScript Version: at least 2.0.0 - 2.3.0

Code

interface ITest1 { IsTest: boolean; }
interface ITest2 { IsTest?: boolean; }

let case1: ITest1 | string;
case1 = case1 as string;
case1.indexOf("_"); // <-- no error

let case2: ITest2 | string;
case2 = case2 as string;
case2.indexOf("_"); // <-- ERROR

Expected behavior:
In the second case there should be no error reported, same as in the first one. case2 variable should have type string after casting case2 = case2 as string

Actual behavior:
When union of types contain interface with all optional properties than casting as one of the types does not change the type of a variable to more specific one.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo la riproduzione in TypeScript mostrata nell’issue e traccia come viene gestita, dopo un’asserzione di tipo, un’unione contenente un’interfaccia con tutte le proprietà opzionali. Aggiungi un test di regressione per i due casi e considera conclusa l’issue quando la seconda variabile viene accettata come string e il primo caso esistente rimane corretto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.