microsoft / microsoft/TypeScript

Type can't `keyof`

Aperta
#56,871 6 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

🔎 Search Terms
  • keyof
🕗 Version & Regression Information
  • This is a crash
⏯ Playground Link

No response

💻 Code
export type IsEqual<A, B> =
  (<T>() => T extends A ? 1 : 2) extends
  (<T>() => T extends B ? 1 : 2) ? true : false

export type IsNotEqual<A, B> =
  (<T>() => T extends A ? 1 : 2) extends
  (<T>() => T extends B ? 1 : 2) ? false : true

export interface Entries<X> {
  100000: [true extends (
    & IsNotEqual<X, ArrayConstructor>
    // Simplifying the problem here allows for the removal of the next line.
    & ([X] extends [ObjectConstructor] ? true : false)
  ) ? true : false, {
    check: (
      & IsNotEqual<X, ArrayConstructor>
      & ([X] extends [ObjectConstructor] ? true : false)
    ),
    x: X
  }]
  [key: number & {}]: [boolean, any]
}
type Mapping<
  X,
  E extends Entries<X> = Entries<X>
> = {
  [ K in keyof E
      as true extends (
        IsEqual<E[K & number][0], true>
      ) ? K : never
  ]: E[K & number][1]
}
type A0 = keyof Mapping<ObjectConstructor>
//   ^? type A0 = 100000
type A1 = ObjectConstructor extends infer T ? Mapping<T> : never
//   ^? type A1 = { 10000: { check: true; x: ObjectConstructor } }
type A2 = ObjectConstructor extends infer T ? keyof Mapping<T> : never
//   ^? type A2 = never
type A3 = [ObjectConstructor] extends [infer T] ? keyof Mapping<T> : never
//   ^? type A3 = never
type A4 = keyof (ObjectConstructor extends infer T ? Mapping<T> : never)
//   ^? type A4 = 11000
Output
export {};
//   ^?

Compiler Options
{
  "compilerOptions": {
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "target": "ES2017",
    "jsx": "react",
    "module": "ESNext",
    "moduleResolution": "node"
  }
}

Playground Link: Provided

🙁 Actual behavior

In the case of A2, the type obtained through the operation cannot be used with keyof to obtain the Keys of the corresponding type.

🙂 Expected behavior
type A2 = 11000
Additional information about the issue

No response

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 fornita in TypeScript Playground e confronta A2 con A0–A4, concentrandoti sulla valutazione di keyof per i tipi condizionali e mappati mostrati. Il lavoro è completato quando il crash è risolto e A2 viene valutato come 11000 senza modificare il comportamento previsto degli esempi adiacenti.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.