microsoft / microsoft/TypeScript

Tuple + Record Intersection Assignment Compatibility Inconsistency

Aperta
#40,269 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

TypeScript Version: 4.1.0-dev.20200824

Search Terms: tuple, object, intersection, type, assignment, coercion

Code

Here is the type that I'd like to use in an assignment:

type X = {example: "Field"} & number[];

The following produces TS error 2322.

const x: X = {
  ...[1, 2],
  example: "Field",
} as const;

If we update this example like so, the type error disappears:

- const x: X = {
+ const x = {
    ...[1, 2],
    example: "Field",
  } as const;

+ const y: X = x;

Expected behavior:

I would have expected the first assignment to not produce a type error. I'm wondering if this inconsistency (if that's the correct word for it) is an edge case, or by design? Apologies if this issue is unhelpful! Thank you!

Actual behavior:

Playground Link: https://www.typescriptlang.org/play?#code/HYQwtgpgzgDiDGEAEAhATgewNYWEg3gLABQSSALgJ4zIAaSAvARAB7gwA2EAXEgEQAxAJYQOAEz4BfJADIkwAK5gARhDQBtALoBuEiTLwMwKOSQte9JkVJkkAOgfqAjABokAJk0v9t1uy68giLifN420iBQSIbG5LrEknrEoJCwCMgA6hhoWFHWZFQ0SJbMbGCcPPzCohLScooqalrxPjEmZowEPmQOds5unmG2SH7lAVXBEkNIEVFtcUkGRu2UFp0s8ZJAA

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 con il TypeScript Playground collegato e riproduci le due forme di assegnazione mostrate nell’issue. Segui il percorso di type-checking per i tipi intersezione, gli spread di array o tuple e le assegnazioni di letterali oggetto; il lavoro è concluso quando il comportamento è spiegato e, se viene confermato come bug, un test di regressione cattura il risultato atteso.

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
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.