microsoft / microsoft/TypeScript

Nested lookuptypes do not inference correctly

Ouverte
#27,709 3 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Bug Domain: Indexed Access Types
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

While trying to scope certain keys that would point to different types, I stumbled upon a problem when I needed to nest lookup types to lookup what type the key would correspond to. During decleration, this did not seem to be possible to get the compiler to understand. Applying the type does actually give out the correct type inferencing though. A simple example can be observed below.

TypeScript Version: 3.2.0-dev.201xxxxx
Tested with both 3.1.1 and typescript@next

Search Terms:
Lookuptypes, type inference, keyof

Code

interface Y {
  x: {
    x1: "x1";
    x2: "x2";
  }
}

interface Z {
  x1: number;
  x2: string;
  x3: number;
}

type H<K extends keyof Y, J extends keyof Y[K]> = (k: K, j: J) => Z[Y[K][J]]; // <----- Error Incorrectly?!
type T6 = H<"x", "x1">;
type T7 = H<"x", "x2">;
type T8 = H<"x", "x3">; // Errors Correctly!

// @ts-ignore
type L<K extends keyof Y, J extends keyof Y[K]> = (k: K, j: J) => Z[Y[K][J]];
type T3 = L<"x", "x1">;
type T4 = L<"x", "x2">;
type T5 = L<"x", "x3">; // Errors Correctly!

Expected behavior:
For the Z[Y[K][J]] in the example to not give error. Applying the type seems to work correctly.

Actual behavior:
Decleration of the type shows error when it should not.

Playground Link: https://www.typescriptlang.org/play/#src=interface%20Y%20%7B%0D%0A%20%20x%3A%20%7B%0D%0A%20%20%20%20x1%3A%20%22x1%22%3B%0D%0A%20%20%20%20x2%3A%20%22x2%22%3B%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Ainterface%20Z%20%7B%0D%0A%20%20x1%3A%20number%3B%0D%0A%20%20x2%3A%20string%3B%0D%0A%20%20x3%3A%20number%3B%0D%0A%7D%0D%0A%0D%0A%0D%0Atype%20H%3CK%20extends%20keyof%20Y%2C%20J%20extends%20keyof%20Y%5BK%5D%3E%20%3D%20(k%3A%20K%2C%20j%3A%20J)%20%3D%3E%20Z%5BY%5BK%5D%5BJ%5D%5D%3B%0D%0A%0D%0Atype%20T6%20%3D%20H%3C%22x%22%2C%20%22x1%22%3E%3B%0D%0Atype%20T7%20%3D%20H%3C%22x%22%2C%20%22x2%22%3E%3B%0D%0Atype%20T8%20%3D%20H%3C%22x%22%2C%20%22x3%22%3E%3B%0D%0A%0D%0A%2F%2F%20%40ts-ignore%0D%0Atype%20L%3CK%20extends%20keyof%20Y%2C%20J%20extends%20keyof%20Y%5BK%5D%3E%20%3D%20(k%3A%20K%2C%20j%3A%20J)%20%3D%3E%20Z%5BY%5BK%5D%5BJ%5D%5D%3B%0D%0A%0D%0Atype%20T3%20%3D%20L%3C%22x%22%2C%20%22x1%22%3E%3B%0D%0Atype%20T4%20%3D%20L%3C%22x%22%2C%20%22x2%22%3E%3B%0D%0Atype%20T5%20%3D%20L%3C%22x%22%2C%20%22x3%22%3E%3B%0D%0A

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par l’exemple TypeScript Playground lié et reproduisez le diagnostic sur l’accès indexé imbriqué dans le type H. Suivez le traitement par le compilateur de Z[Y[K][J]] pour les types de recherche génériques ; le travail est terminé lorsque la déclaration est acceptée, tandis que le cas invalide T8 signale toujours une erreur.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
38/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.