microsoft / microsoft/TypeScript

False positive for Circular dependency error

Aperta
#54,204 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: check: Type Circularity Help Wanted Possible Improvement
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

Hello. First of all want to express my love to TypeScript and admit of all work you did for the best typing system I've ever known.

I believe I found a bug where three points are touched: type inference, recursive types and variadic params. The bug doesn't happen when you take off any of this points. I put the reproduction code below.

I believe that it's a bug because the main recursive feature works fine for composite that has slightly complicated signature. Also, I can make failing working by making a union with a dummy type, so it's

failing: AComponent<[ARows | 'never']>

The bug doesn't happen when I have the direct components type without infering:

type AComponentTypes = {
  simple: [string],
  composite: [string, ARows]
  failing: [ARows]
}

The bug doesn't happen when I don't use variadic types:

type ARowUnion<U> = U extends keyof AComponents ? [U, AArgument<AComponents[U]>] : never

Is there any other workarounds besides defining a unionARows | 'never' ?

I spent about 4 hours struggling with this so I hope this is a helpful finding. Thank you

🔎 Search Terms

typescript, generic types, variadic types, composition, recursion, self reference, mapped types, false positive

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about
  • I was unable to test this on prior versions because variadic types were not implemented.
  • There is no working version. 4.0.5. still has the problem, but 3.9.7 seems not supporting variadic types.
⏯ Playground Link

Playground link with relevant code

💻 Code
type AComponents = {
  simple: AComponent<[string]>,
  composite: AComponent<[string, ARows]>
  failing: AComponent<[ARows]>
}

type AComponent<T extends unknown[]> = (args: T) => void

type AArgument<T> = T extends AComponent<infer V> ? V : never

type ARowUnion<U> = U extends keyof AComponents ? [U, ...AArgument<AComponents[U]>] : never

type ARow = ARowUnion<keyof AComponents>
type ARows = ARow[]

const rows: ARow[] = [
  ['simple', 'Hello world'],
  ['composite', 'my param', [
    ['simple', 'Hello world'],
  ]],
  ['failing', [
    ['simple', 'Hello world'],
  ]]
]
🙁 Actual behavior

Type alias 'ARow' circularly references itself.
Type arguments for 'Array' circularly reference themselves.

🙂 Expected behavior

There is no errors for the types definitions, both failing and component types are valid and allows recursive structures.

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 dal TypeScript Playground collegato e riproduci le diagnosi relative ai riferimenti circolari utilizzando i tipi tupla ricorsivi, inferiti e variadici forniti. Confronta le definizioni che falliscono con le varianti funzionanti elencate, quindi analizza il comportamento pertinente del controllo dei tipi. Il lavoro è completo quando la struttura ricorsiva valida non segnala più errori di riferimento circolare e la regressione è coperta da un test appropriato del compilatore.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.