microsoft / microsoft/TypeScript

Incorrect inference of mapped substitution type in conditional involving a union of arrays (regression)

Aperta
#56,018 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: Conditional Types Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 Search Terms

"inference", "conditional type", "substitution type", "mapped type", "type constraint", "array", "union type"

🕗 Version & Regression Information

This changed between versions 5.0.4 and 5.1.6

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.1.6#code/C4TwDgpgBAShB2BDAthAJlAvFAThRaA9vADYhQAUA3lANoDWAXFAAo6GQ6gDSEIAuswDOwHAEt4AcygBfKAB8oAV3hoIAMwnoAlLX4AofaEhQAYoUIAeACpQIAD2AI0Q3PiKlyFNhwhcQvOSKKmqa8Dp6CrAIKOgAfFj6UMlQtg5Oqq5wSKgYAPxQAOIQwIFClnAAjkpieGg2cQnMVTV1DYbG0MWlfOUwdo7OWTG5Cdg0tNxQElD0fITqsIKz84swk-yyhgD028pCiJIQRuDQAIJYZhaWtADkiLd5-Am7dABEiG8GnVAAQpfmKy0GiIPLMW4AI1usmeUFetA+XyAA

💻 Code
type Renamed = readonly ({ [k: PropertyKey]: string } | undefined)[]

type Foo<T extends readonly (PropertyKey | undefined)[] | Renamed> =
    T extends Renamed ? GetKeys<Required<T>> : Required<T>
//                              ~~~~~~~~~~
// Type 'Required<T>' does not satisfy the constraint 'Renamed'

type GetKeys<R extends Renamed> = { [K in keyof R]: keyof R[K] }

// usage
type A = Foo<['a'?]> // ["a"]
type B = Foo<[{ a?: 'b' }]> // ["a"]
🙁 Actual behavior

TS fails to see that Required<T extends Renamed> extends Renamed.

🙂 Expected behavior

It should compile.

Additional information about the issue

Substitute readonly (PropertyKey | undefined)[] with something that is not an array and the problem disappears.

I get the same error when I do GetKeys<Id<T>> with type Id<T> = { [K in keyof T]: T[K] }.

Extract<Required<T>, Required<Renamed>> silences the error, but Extract<Required<T>, Renamed> does not.

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 la riproduzione collegata in TypeScript Playground e confronta il comportamento tra le versioni 5.0.4 e 5.1.6. Analizza l'inferenza dei tipi condizionali e mappati per l'unione di array, quindi verifica che gli esempi forniti Foo, A e B vengano compilati con i risultati attesi senza dover usare Extract.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.