microsoft / microsoft/TypeScript

Can't infer the type of this self-calculated

Aperta
#54,766 0 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
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

🔎 Search Terms
  • infer
  • self-calculated
  • generic
🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about Type Inference

⏯ Playground Link
type Exactify<T, X extends T> = T & {
  [K in keyof X]: K extends keyof T ? X[K] : never
}

declare function pick<T, X extends Exactify<{
  [K in keyof T]?: true
}, X>>(t: T, x: X): [Pick<T, keyof X>,...

Playground Link

💻 Code
type Exactify<T, X extends T> = T & {
  [K in keyof X]: K extends keyof T ? X[K] : never
}

declare function pick<T, X extends Exactify<{
  [K in keyof T]?: true
}, X>>(t: T, x: X): [Pick<T, keyof X>, X, X extends Exactify<infer A, infer B> ? B : never]

const t0 = pick({ a: '1', b: 1 }, { a: true })
const [
  a0,
  a1,
// ^? Exactify<{ a?: string | undefined; b?: number | undefined }, { a: boolean }>
  a2
// ^? { a?: true | undefined; b?: true | undefined }
] = pick({ a: '1', b: 1 }, { a: true })
🙁 Actual behavior

The a2 variable is { a?: true | undefined; b?: true | undefined } type.

🙂 Expected behavior

Infer the a2 variable is { a: boolean } or { a: true } type.

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 dalla riproduzione collegata in TypeScript Playground e dall’esempio generico pick/Exactify nell’issue. Analizza perché il tipo a2 inferito è Exactify<...> invece di { a: boolean } o { a: true }, quindi verifica l’inferenza prevista con un test mirato del compilatore.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
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.