microsoft / microsoft/TypeScript

Type `[x, x]` is not assignable to type `x[] & { length: 2; }`

Aperta
#44,508 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

This error is probably self-explanatory. Either this is supposed to work and it’s a bug, or I’m misunderstanding something in which case please feel free to close.

Type '(Record<string, any> | null)[] & { length: 2; }' is not assignable to type '[Record<string, any> | null, Record<string, any> | null]'.ts(2322)

Outer function is typed to return the latter. It calls an inner function, typed to return the former. One would assume this should work.

Possibly related: #44309

🔎 Search Terms

array literal length

🕗 Version & Regression Information

TypeScript 4.1.3

💻 Code
async function readObjectVersions
<L extends number, C extends string[] & { length: L }>
(objectPath: string, versions: C):
Promise<(Record<string, any> | null)[] & { length: L }> {
  return versions.map(version => ({ foo: 'bar' }));
}

const version1: string = 'x';
const version2: string = 'y';

async function _readObjectVersions(objectPath: string):
Promise<[ Record<string, any> | null, Record<string, any> | null ]> {
  return await readObjectVersions(objectPath, [version1, version2]);
}

_readObjectVersions('abc');
🙁 Actual behavior

The error as described above.

🙂 Expected behavior

No type error.

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 riproducendo l'errore con le funzioni readObjectVersions e _readObjectVersions nell'esempio TypeScript fornito, usando TypeScript 4.1.3. Traccia il modo in cui l'array mappato e il suo vincolo di lunghezza vengono verificati rispetto al tipo di ritorno tupla; il lavoro è completato quando la chiamata mostrata supera il controllo dei tipi senza l'errore segnalato.

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
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.